Suppose the following code:
# We define a base for all DB objects, currently empty.
class _Base:
pass
Base = declarative_base(cls=_Base, metadata=MetaData(naming_convention=…))
# Then the objects.
class User(Base):
__tablename__ = "users"
id = Column(UUID(), default=uuid.uuid4, primary_key=True)
…
For the majority of code this works well and the id is initialized whenever
the object is committed. However, there are cases when I need to get a hold
of a new object’s id and that happens before the commit. In such cases the
id is not set yet, and I have extra code which sets the id manually.
That feels crummy to me.
Now I wonder if that’s poor implementation because a new object should
always be committed before use, or if I should perhaps expand the _Base
class, for example:
class _Base:
def __init__(self, *args, **kwargs):
super().__init__(args, kwargs)
if hasargs(self, "id"):
self.id = uuid.uuid4()
That way, every object would have an id assigned and if the object is
loaded from the db then that initial id would be overwritten. Not pretty
either, but maybe less crummy than the current implementation.
What are your thoughts?
Much thanks!
Jens
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.