保存创建者、更新者和删除者信息
Save the created by, updated by and deleted by information
我试图在我的每个表中保存有关记录的创建、更新或删除的信息。
目前我有:
class User(Base):
__tablename__ = "user"
id = Column(UUID, primary_key=True, index=True)
civil_title = Column(Enum(EnumTitle))
lastname = Column(String, index=True)
firstname = Column(String)
type = Column(Enum(EnumTypeUser))
hashed_password = Column(String, nullable=False)
is_active = Column(Boolean(), default=True)
is_superuser = Column(Boolean(), default=False)
created_at = Column(TIMESTAMP(timezone=False),server_default=func.now(), nullable=False)
created_by = Column(UUID, ForeignKey("user.id"))
created_by_rl = relationship("User", foreign_keys=[created_by])
updated_at = Column(DateTime)
updated_by = Column(UUID, ForeignKey("user.id"))
updated_by_rl = relationship("User", foreign_keys=[updated_by])
deleted_at = Column(DateTime)
deleted_by = Column(UUID, ForeignKey("user.id"))
deleted_by_rl = relationship("User", foreign_keys=[deleted_by])
practice_id = Column(UUID, ForeignKey("practice.id"))
practice = relationship("Practice", foreign_keys=[practice_id], back_populates="employees")
address_id = Column(UUID, ForeignKey("user_address.id"))
address = relationship("UserAddress",foreign_keys=[address_id], back_populates="users")
# items = relationship("Item", back_populates="owner")
contacts = relationship("UserContactDetail", back_populates="user")
schedules = relationship("Schedule", back_populates="user")
__mapper_args__ = {
"polymorphic_identity":"user",
"polymorphic_on":type
}
和
class UserAddress(Base):
__tablename__ = "user_address"
id = Column(UUID, primary_key=True, index=True)
street = Column(String)
streetnumber = Column(String)
town = Column(String)
cp = Column(Integer)
created_at = Column(TIMESTAMP(timezone=False),server_default=func.now(), nullable=False)
created_by = Column(UUID, ForeignKey("user.id"))
created_by_rl = relationship("User", foreign_keys=[created_by])
updated_at = Column(DateTime)
updated_by = Column(UUID, ForeignKey("user.id"))
updated_by_rl = relationship("User", foreign_keys=[updated_by])
deleted_at = Column(DateTime)
deleted_by = Column(UUID, ForeignKey("user.id"))
deleted_by_rl = relationship("User", foreign_keys=[deleted_by])
users = relationship("User", back_populates="address")
我遇到以下错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'user_address' and 'user'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
如何确保不会发生此错误?
做这样的事情是个好习惯吗?
您正在向所有这些列添加错误定义的关系,这就是该错误的原因。为了解决你的问题,我会尝试这样的事情:
class TrackTimeMixin:
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=datetime.now)
class SoftDeleteMixin:
deleted_at = Column(DateTime, nullable=True)
def soft_delete(self):
self.deleted_at = datetime.now()
class User(Base, TrackTimeMixin, SoftDeleteMixin):
"""define other columns"""
class UserAddress(Base, TrackTimeMixin, SoftDeleteMixin):
"""define other columns"""
TrackTimeMixin
用于 created_at
和 updated_at
列,它应该
自动工作。
SoftDeleteMixin
添加了 deleted_at
列,您需要添加更多逻辑才能使其正常工作。
我有意将其分成 2 个混入,因为对于其他一些模型,您可能只想使用其中一个或 none。所以现在,当你想删除一个用户时,你不必这样做:
session.delete(user)
session.commit()
你实际上会这样做:
user.soft_delete()
session.commit()
您还需要一个逻辑来获取未删除的用户,但从这一点开始应该很容易实现。
我试图在我的每个表中保存有关记录的创建、更新或删除的信息。
目前我有:
class User(Base):
__tablename__ = "user"
id = Column(UUID, primary_key=True, index=True)
civil_title = Column(Enum(EnumTitle))
lastname = Column(String, index=True)
firstname = Column(String)
type = Column(Enum(EnumTypeUser))
hashed_password = Column(String, nullable=False)
is_active = Column(Boolean(), default=True)
is_superuser = Column(Boolean(), default=False)
created_at = Column(TIMESTAMP(timezone=False),server_default=func.now(), nullable=False)
created_by = Column(UUID, ForeignKey("user.id"))
created_by_rl = relationship("User", foreign_keys=[created_by])
updated_at = Column(DateTime)
updated_by = Column(UUID, ForeignKey("user.id"))
updated_by_rl = relationship("User", foreign_keys=[updated_by])
deleted_at = Column(DateTime)
deleted_by = Column(UUID, ForeignKey("user.id"))
deleted_by_rl = relationship("User", foreign_keys=[deleted_by])
practice_id = Column(UUID, ForeignKey("practice.id"))
practice = relationship("Practice", foreign_keys=[practice_id], back_populates="employees")
address_id = Column(UUID, ForeignKey("user_address.id"))
address = relationship("UserAddress",foreign_keys=[address_id], back_populates="users")
# items = relationship("Item", back_populates="owner")
contacts = relationship("UserContactDetail", back_populates="user")
schedules = relationship("Schedule", back_populates="user")
__mapper_args__ = {
"polymorphic_identity":"user",
"polymorphic_on":type
}
和
class UserAddress(Base):
__tablename__ = "user_address"
id = Column(UUID, primary_key=True, index=True)
street = Column(String)
streetnumber = Column(String)
town = Column(String)
cp = Column(Integer)
created_at = Column(TIMESTAMP(timezone=False),server_default=func.now(), nullable=False)
created_by = Column(UUID, ForeignKey("user.id"))
created_by_rl = relationship("User", foreign_keys=[created_by])
updated_at = Column(DateTime)
updated_by = Column(UUID, ForeignKey("user.id"))
updated_by_rl = relationship("User", foreign_keys=[updated_by])
deleted_at = Column(DateTime)
deleted_by = Column(UUID, ForeignKey("user.id"))
deleted_by_rl = relationship("User", foreign_keys=[deleted_by])
users = relationship("User", back_populates="address")
我遇到以下错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'user_address' and 'user'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
如何确保不会发生此错误? 做这样的事情是个好习惯吗?
您正在向所有这些列添加错误定义的关系,这就是该错误的原因。为了解决你的问题,我会尝试这样的事情:
class TrackTimeMixin:
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=datetime.now)
class SoftDeleteMixin:
deleted_at = Column(DateTime, nullable=True)
def soft_delete(self):
self.deleted_at = datetime.now()
class User(Base, TrackTimeMixin, SoftDeleteMixin):
"""define other columns"""
class UserAddress(Base, TrackTimeMixin, SoftDeleteMixin):
"""define other columns"""
TrackTimeMixin
用于 created_at
和 updated_at
列,它应该
自动工作。
SoftDeleteMixin
添加了 deleted_at
列,您需要添加更多逻辑才能使其正常工作。
我有意将其分成 2 个混入,因为对于其他一些模型,您可能只想使用其中一个或 none。所以现在,当你想删除一个用户时,你不必这样做:
session.delete(user)
session.commit()
你实际上会这样做:
user.soft_delete()
session.commit()
您还需要一个逻辑来获取未删除的用户,但从这一点开始应该很容易实现。