discard adjustable models' id prefix & payload length

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