From d12de3d532d4e89cecc8dfaaec4ac8bcfebc7a11 Mon Sep 17 00:00:00 2001 From: -LAN- Date: Tue, 20 May 2025 16:25:33 +0800 Subject: [PATCH] fix: Add reconstructor to initialize transient fields Introduces a reconstructor method to ensure that transient fields such as 'role' and '_current_tenant' are initialized upon loading an instance of the Account model. This change improves the object's state consistency, especially when instances are reloaded from the database, ensuring these fields are set to 'None' by default. Signed-off-by: -LAN- --- api/models/account.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/models/account.py b/api/models/account.py index cc2c30202f..7ffeefa980 100644 --- a/api/models/account.py +++ b/api/models/account.py @@ -4,7 +4,7 @@ from typing import Optional, cast from flask_login import UserMixin # type: ignore from sqlalchemy import func -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, mapped_column, reconstructor from models.base import Base @@ -81,8 +81,6 @@ class AccountStatus(enum.StrEnum): class Account(UserMixin, Base): - role: Optional["TenantAccountRole"] = None - _current_tenant: Optional["Tenant"] = None __tablename__ = "accounts" __table_args__ = (db.PrimaryKeyConstraint("id", name="account_pkey"), db.Index("account_email_idx", "email")) @@ -103,6 +101,11 @@ class Account(UserMixin, Base): created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) + @reconstructor + def init_on_load(self): + self.role: Optional[TenantAccountRole] = None + self._current_tenant: Optional[Tenant] = None + @property def is_password_set(self): return self.password is not None