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
+22 -8
View File
@@ -37,12 +37,23 @@ class Document(UUIDPrimaryKeyMixin, TimestampMixin, Base):
original_filename: Mapped[str] = mapped_column(
String(512),
nullable=False,
comment="原始文件名 (仅展示)",
comment="原始文件名或文本标题 (仅展示)",
)
storage_path: Mapped[str] = mapped_column(
storage_path: Mapped[str | None] = mapped_column(
String(1024),
nullable=True,
comment="相对于 data/ 的物理存储路径(文本内容文档为 NULL)",
)
content: Mapped[str | None] = mapped_column(
Text,
nullable=True,
comment="直接输入的文本内容(非文件上传时使用)",
)
content_format: Mapped[str] = mapped_column(
String(16),
default="markdown",
nullable=False,
comment="相对于 data/ 的物理存储路径",
comment="内容格式:markdown / text",
)
markdown_path: Mapped[str | None] = mapped_column(
String(1024),
@@ -51,23 +62,26 @@ class Document(UUIDPrimaryKeyMixin, TimestampMixin, Base):
)
file_size: Mapped[int] = mapped_column(
Integer,
default=0,
nullable=False,
comment="文件大小 (字节)",
comment="文件大小 (字节),文本内容文档为内容长度",
)
mime_type: Mapped[str] = mapped_column(
String(127),
default="text/markdown",
nullable=False,
comment="MIME 类型",
)
file_ext: Mapped[str] = mapped_column(
String(16),
default=".md",
nullable=False,
comment="文件扩展名 (.docx/.pdf)",
comment="文件扩展名 (.docx/.pdf/.md)",
)
sha256: Mapped[str] = mapped_column(
sha256: Mapped[str | None] = mapped_column(
String(64),
nullable=False,
comment="文件 SHA-256 哈希",
nullable=True,
comment="文件 SHA-256 哈希(文本内容文档为 NULL",
)
doc_token_hash: Mapped[str] = mapped_column(
String(64),
+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}>"