PMFlow/backend/app/models/base.py

45 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""核心基类
Tenant 表无 tenant_id(自身即租户),使用 BaseNoTenant。
其他表继承 Base(含 tenant_id、created_at、updated_at)。
"""
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy import TIMESTAMP, func, BigInteger
from typing import Optional
class BaseNoTenant(DeclarativeBase):
"""无 tenant_id 的基类,用于 Tenant 表"""
__abstract__ = True
created_at: Mapped[Optional[TIMESTAMP]] = mapped_column(
TIMESTAMP, nullable=False, server_default=func.now()
)
updated_at: Mapped[Optional[TIMESTAMP]] = mapped_column(
TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now()
)
class Base(BaseNoTenant):
"""带 tenant_id 的基类,所有租户表继承此类"""
__abstract__ = True
tenant_id: Mapped[int] = mapped_column(
BigInteger, nullable=False, default=1, index=True
)
created_at: Mapped[Optional[TIMESTAMP]] = mapped_column(
TIMESTAMP, nullable=False, server_default=func.now()
)
updated_at: Mapped[Optional[TIMESTAMP]] = mapped_column(
TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now()
)
class BaseNoCreatedAt(BaseNoTenant):
"""仅有 tenant_id 和 updated_at,无 created_at(如 user_workloads)"""
__abstract__ = True
tenant_id: Mapped[int] = mapped_column(
BigInteger, nullable=False, default=1, index=True
)
updated_at: Mapped[Optional[TIMESTAMP]] = mapped_column(
TIMESTAMP, nullable=False, server_default=func.now(), onupdate=func.now()
)