diff --git a/app/models/base.py b/app/models/base.py index b25d628..0c8d8cf 100644 --- a/app/models/base.py +++ b/app/models/base.py @@ -56,22 +56,24 @@ class AutoID: 不再需要手动定义任何 prefix,全自动根据 __tablename__ 编码生成 支持子类自定义长度 """ - # 默认配置 - _id_prefix_len = 3 # 前缀长度 - _id_payload_len = 22 # 主体长度 + # 全局固定配置 + ID_PREFIX_LEN = 3 + ID_PAYLOAD_LEN = 22 @classmethod def id_length(cls) -> int: """返回当前类 ID 的总长度""" - return cls._id_prefix_len + cls._id_payload_len + return cls.ID_PREFIX_LEN + cls.ID_PAYLOAD_LEN @declared_attr def id(cls): # 运行时动态获取子类的 __tablename__ tablename = cls.__tablename__ + # 获取子类配置的长度 - p_len = cls._id_prefix_len - s_len = cls._id_payload_len + p_len = AutoID.ID_PREFIX_LEN + s_len = AutoID.ID_PAYLOAD_LEN + prefix = encode_tablename_to_prefix(tablename, length=p_len) # 核心工厂函数:拼接【编码后的表前缀】和【递增Payload】 diff --git a/app/models/follow.py b/app/models/follow.py index c9daeb4..c81a894 100644 --- a/app/models/follow.py +++ b/app/models/follow.py @@ -1,25 +1,18 @@ -from typing import TYPE_CHECKING from sqlalchemy import Column, DateTime, String, ForeignKey, UniqueConstraint from sqlalchemy.sql import func from .base import AutoID, Base -# 只有在静态检查时才导入 -# 运行阶段这段代码会被跳过,彻底避免循环导入 -# 不确定是否用得上,但感觉这是个好习惯,尤其在模型之间有外键关系时 -if TYPE_CHECKING: - from .user import User - class Follow(Base, AutoID): """用户关注关系表""" __tablename__ = "follows" follower_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), index=True ) followee_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), index=True ) diff --git a/app/models/interaction.py b/app/models/interaction.py index 702c8ce..921d9ed 100644 --- a/app/models/interaction.py +++ b/app/models/interaction.py @@ -1,24 +1,18 @@ -from typing import TYPE_CHECKING from sqlalchemy import Column, DateTime, String, ForeignKey, UniqueConstraint from sqlalchemy.sql import func from .base import AutoID, Base -if TYPE_CHECKING: - from .user import User - from .project import ProjectItem - from .workspace import Workspace - class ProjectLike(Base, AutoID): """项目点赞表""" __tablename__ = "project_likes" user_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), index=True ) project_item_id = Column( - String(ProjectItem.id_length()), + String(AutoID.id_length()), ForeignKey("project_items.id", ondelete="CASCADE"), index=True ) @@ -34,12 +28,12 @@ class ProjectFavorite(Base, AutoID): __tablename__ = "project_favorites" user_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), index=True ) project_item_id = Column( - String(ProjectItem.id_length()), + String(AutoID.id_length()), ForeignKey("project_items.id", ondelete="CASCADE"), index=True ) @@ -55,12 +49,12 @@ class ProjectShare(Base, AutoID): __tablename__ = "project_shares" user_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), index=True ) project_item_id = Column( - String(ProjectItem.id_length()), + String(AutoID.id_length()), ForeignKey("project_items.id", ondelete="CASCADE"), index=True ) diff --git a/app/models/library.py b/app/models/library.py index 6f75e90..b3e21b3 100644 --- a/app/models/library.py +++ b/app/models/library.py @@ -1,18 +1,13 @@ -from typing import TYPE_CHECKING from sqlalchemy import Column, DateTime, String, ForeignKey, BigInteger from sqlalchemy.sql import func from .base import AutoID, Base -if TYPE_CHECKING: - from .user import User - from .workspace import Workspace - class LibraryItem(Base, AutoID): """工作区共享媒体/文件资源库""" __tablename__ = "library_items" workspace_id = Column( - String(Workspace.id_length()), + String(AutoID.id_length()), ForeignKey("workspaces.id", ondelete="CASCADE"), index=True, ) @@ -34,7 +29,7 @@ class LibraryItem(Base, AutoID): mime_type = Column(String, nullable=True) # 媒体类型,例如 'image/jpeg', 'video/mp4', 'application/pdf' uploaded_by = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="SET NULL"), nullable=True, ) diff --git a/app/models/project.py b/app/models/project.py index 63f5ccd..5ca9241 100644 --- a/app/models/project.py +++ b/app/models/project.py @@ -1,17 +1,13 @@ -from typing import TYPE_CHECKING from sqlalchemy import Column, DateTime, String, ForeignKey, Integer from sqlalchemy.sql import func from .base import AutoID, Base -if TYPE_CHECKING: - from .workspace import Workspace - class ProjectItem(Base, AutoID): """项目与文件夹的树状虚拟文件系统""" __tablename__ = "project_items" workspace_id = Column( - String(Workspace.id_length()), + String(AutoID.id_length()), ForeignKey("workspaces.id", ondelete="CASCADE"), index=True, ) diff --git a/app/models/workspace.py b/app/models/workspace.py index 6a42ea9..74b428a 100644 --- a/app/models/workspace.py +++ b/app/models/workspace.py @@ -1,11 +1,7 @@ -from typing import TYPE_CHECKING from sqlalchemy import Column, DateTime, String, ForeignKey, BigInteger, UniqueConstraint, Integer from sqlalchemy.sql import func from .base import AutoID, Base -if TYPE_CHECKING: - from .user import User - class Workspace(Base, AutoID): """工作区表""" __tablename__ = "workspaces" @@ -34,13 +30,13 @@ class WorkspaceMember(Base, AutoID): __tablename__ = "workspace_members" workspace_id = Column( - String(Workspace.id_length()), + String(AutoID.id_length()), ForeignKey("workspaces.id", ondelete="CASCADE"), index=True, ) # 允许 user_id 为空,因为被邀请的人可能还没注册系统,我们先记录他的邮箱 user_id = Column( - String(User.id_length()), + String(AutoID.id_length()), ForeignKey("users.id", ondelete="CASCADE"), nullable=True, index=True,