This commit is contained in:
amb
2026-09-01 20:51:46 +08:00
parent 70ece67910
commit b80161972a
18 changed files with 1878 additions and 359 deletions
+27 -5
View File
@@ -1,15 +1,16 @@
"""DocumentCategory 文档分类模型。"""
"""DocumentCategory 文档分类模型(支持树形目录结构)"""
from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy import Boolean, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
class DocumentCategory(UUIDPrimaryKeyMixin, TimestampMixin, Base):
"""文档分类模型。
"""文档分类模型(树形结构)
技术审查 §2.2document_categories 表
通过 parent_id 实现层级关系,path 用于高效查询子树
is_folder=True 表示文件夹(可包含子项),False 表示叶子分类。
"""
__tablename__ = "document_categories"
@@ -21,11 +22,30 @@ class DocumentCategory(UUIDPrimaryKeyMixin, TimestampMixin, Base):
index=True,
comment="所属知识库 ID",
)
parent_id: Mapped[str | None] = mapped_column(
String(32),
ForeignKey("document_categories.id"),
nullable=True,
index=True,
comment="父分类 IDNULL = 顶层)",
)
name: Mapped[str] = mapped_column(
String(255),
nullable=False,
comment="分类名称",
)
path: Mapped[str] = mapped_column(
Text,
default="/",
nullable=False,
comment="物化路径,如 /01_公司层/04_岗位AI角色/",
)
is_folder: Mapped[bool] = mapped_column(
Boolean,
default=True,
nullable=False,
comment="True=文件夹(可含子项),False=叶子分类",
)
sort_order: Mapped[int] = mapped_column(
Integer,
default=0,
@@ -35,7 +55,9 @@ class DocumentCategory(UUIDPrimaryKeyMixin, TimestampMixin, Base):
# 关系
knowledge_base = relationship("KnowledgeBase", back_populates="categories", lazy="selectin")
parent = relationship("DocumentCategory", remote_side="DocumentCategory.id", lazy="selectin")
children = relationship("DocumentCategory", back_populates="parent", lazy="selectin")
documents = relationship("Document", back_populates="category", lazy="selectin")
def __repr__(self) -> str:
return f"<DocumentCategory {self.name!r} (kb={self.knowledge_base_id!r})>"
return f"<DocumentCategory {self.name!r} path={self.path!r}>"