a5f884f440
Two tables were named after the concept they came from rather than the module
they belong to, so the schema read as if the template tables were part of the
paper module. Renamed (data preserved, `RENAME TABLE` moves rows in place):
paper_template -> template the 模板 module
section_field -> template_field_library the 字段库 the 模板 module owns
The paper tables and `template_field` already followed the rule. The rename
carries through everything that named a module:
models Template, TemplateField, TemplateFieldLibrary
schemas Template*, TemplateFieldLibrary*
crud app/crud/template.py, app/crud/template_field_library.py
API /template-field-library (was /section-fields); handlers are now
named after library entries, which removes the ambiguity with
TemplateField — a placement, a different thing entirely
client src/api/templateFieldLibrary.ts
`paper_template_filed_sort` is deliberately untouched: it is a column of the
paper module, spelled as the feature was specified.
TiDB v8.5 with tidb_enable_foreign_key on — as this cluster runs — enforces
foreign keys rather than ignoring them, so the docs' "TiDB does not enforce
foreign keys" was wrong. Corrected, with what actually follows from it: the
rename was rehearsed (RENAME TABLE carries a referencing constraint along), the
API keeps checking first so a violation names the row instead of surfacing a
driver error, and the ORM cascades stay so behaviour does not depend on a
cluster setting.
Revision f27a1c6d9e04 verified both ways; 40 smoke checks, type-check and build
all pass.
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
"""Templates (模板表) — the outlines a paper can be written against.
|
|
|
|
A template is a named, ordered selection of library fields — the outline a
|
|
paper is written against. It stores no typography of its own: font size and
|
|
colour are read from the referenced
|
|
:class:`~app.models.template_field_library.TemplateFieldLibrary`,
|
|
so correcting a field's styling updates every template that uses it.
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import String, Text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.db.base import Base
|
|
from app.models.mixins import TimestampMixin
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - typing only
|
|
from app.models.template_field import TemplateField
|
|
|
|
|
|
class Template(TimestampMixin, Base):
|
|
"""A named outline: a template name, a summary, and its ordered fields."""
|
|
|
|
__tablename__ = "template"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
|
|
#: Free-text abstract (摘要) describing when to use this template.
|
|
abstract: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
#: The template's fields, always handed to callers in display order.
|
|
#:
|
|
#: ``delete-orphan`` is doing real work here: TiDB parses but does not
|
|
#: enforce ``ON DELETE CASCADE``, so removing a template's rows in the join
|
|
#: table is the ORM's job, not the database's.
|
|
items: Mapped[list["TemplateField"]] = relationship(
|
|
back_populates="template",
|
|
cascade="all, delete-orphan",
|
|
order_by="TemplateField.sort, TemplateField.id",
|
|
lazy="selectin",
|
|
)
|
|
|
|
def __repr__(self) -> str: # pragma: no cover - debugging aid
|
|
return f"<Template id={self.id} name={self.name!r}>"
|