From 50fa39be4f1ad7484f34e5ab3c95317b48ceb73a Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 2 Dec 2021 18:28:32 +0100 Subject: [PATCH] feat(users): Add some more relationships to model --- flaschengeist/models/user.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/flaschengeist/models/user.py b/flaschengeist/models/user.py index 87fdb91..67c4142 100644 --- a/flaschengeist/models/user.py +++ b/flaschengeist/models/user.py @@ -63,16 +63,21 @@ class User(db.Model, ModelSerializeMixin): roles: list[str] = [] permissions: Optional[list[str]] = None + # Protected stuff for backend use only id_ = db.Column("id", Serial, primary_key=True) roles_: list[Role] = db.relationship("Role", secondary=association_table, cascade="save-update, merge") - sessions_ = db.relationship("Session", back_populates="user_") + sessions_: list["Session"] = db.relationship( + "Session", back_populates="user_", cascade="all, delete, delete-orphan" + ) avatar_: Optional[Image] = db.relationship("Image", cascade="all, delete, delete-orphan", single_parent=True) + reset_requests_: list["_PasswordReset"] = db.relationship("_PasswordReset", cascade="all, delete, delete-orphan") + # Private stuff for internal use _avatar_id = db.Column("avatar", Serial, db.ForeignKey("image.id")) _attributes = db.relationship( "_UserAttribute", collection_class=attribute_mapped_collection("name"), - cascade="all, delete", + cascade="all, delete, delete-orphan", ) @property @@ -117,7 +122,7 @@ class _PasswordReset(db.Model): __tablename__ = "password_reset" _user_id: User = db.Column("user", Serial, db.ForeignKey("user.id"), primary_key=True) - user: User = db.relationship("User", foreign_keys=[_user_id]) + user: User = db.relationship("User", back_populates="reset_requests_", foreign_keys=[_user_id]) token: str = db.Column(db.String(32)) expires: datetime = db.Column(UtcDateTime)