You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, MetaData, String
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
|
|
naming_convention = {
|
|
"ix": "ix_%(column_0_label)s",
|
|
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
|
"ck": "ck_%(table_name)s_%(column_0_name)s",
|
|
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
|
"pk": "pk_%(table_name)s",
|
|
}
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
metadata = MetaData(naming_convention=naming_convention)
|
|
|
|
|
|
class IdMixin:
|
|
id: Mapped[str] = mapped_column(
|
|
String(36),
|
|
primary_key=True,
|
|
default=lambda: uuid.uuid4().hex,
|
|
comment="标识",
|
|
)
|
|
|
|
|
|
class TimestampMixin:
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime,
|
|
default=datetime.now,
|
|
nullable=False,
|
|
comment="创建时间",
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime,
|
|
default=datetime.now,
|
|
onupdate=datetime.now,
|
|
nullable=False,
|
|
comment="更新时间",
|
|
)
|
|
|
|
|
|
class AuditMixin:
|
|
created_by: Mapped[str | None] = mapped_column(
|
|
String(36), nullable=True, index=True, comment="创建人"
|
|
)
|
|
updated_by: Mapped[str | None] = mapped_column(
|
|
String(36), nullable=True, index=True, comment="更新人"
|
|
)
|