第二步

This commit is contained in:
amb
2026-09-01 12:24:43 +08:00
parent 47bf6cc5ca
commit 1d8621717a
14 changed files with 815 additions and 9 deletions
+65
View File
@@ -0,0 +1,65 @@
"""KnowledgeBase 知识库模型。"""
from sqlalchemy import Boolean, ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin
class KnowledgeBase(UUIDPrimaryKeyMixin, TimestampMixin, Base):
"""知识库模型。
技术审查 §2.2knowledge_bases 表。
"""
__tablename__ = "knowledge_bases"
user_id: Mapped[str] = mapped_column(
String(32),
ForeignKey("users.id"),
nullable=False,
index=True,
comment="所有者用户 ID",
)
name: Mapped[str] = mapped_column(
String(255),
nullable=False,
comment="知识库名称",
)
description: Mapped[str | None] = mapped_column(
Text,
nullable=True,
comment="知识库描述",
)
enabled: Mapped[bool] = mapped_column(
Boolean,
default=True,
nullable=False,
comment="是否启用",
)
token_hash: Mapped[str] = mapped_column(
String(64),
unique=True,
nullable=False,
index=True,
comment="SHA-256(secret_token) 十六进制",
)
token_encrypted: Mapped[str | None] = mapped_column(
Text,
nullable=True,
comment="Fernet 加密的 token 原文",
)
token_hint: Mapped[str | None] = mapped_column(
String(16),
nullable=True,
comment="token 末 8 位明文,供后台识别",
)
# 关系
user = relationship("User", back_populates="knowledge_bases", lazy="selectin")
documents = relationship("Document", back_populates="knowledge_base", lazy="selectin")
categories = relationship("DocumentCategory", back_populates="knowledge_base", lazy="selectin")
access_logs = relationship("AccessLog", back_populates="knowledge_base", lazy="selectin")
def __repr__(self) -> str:
return f"<KnowledgeBase {self.name!r} (user={self.user_id!r})>"