82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
"""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.2:knowledge_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="是否启用",
|
||
)
|
||
status: Mapped[str] = mapped_column(
|
||
String(16),
|
||
default="active",
|
||
nullable=False,
|
||
comment="状态 (active/deleted)",
|
||
)
|
||
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 位明文,供后台识别",
|
||
)
|
||
token_expires_at: Mapped[str | None] = mapped_column(
|
||
String(32),
|
||
nullable=True,
|
||
comment="链接过期时间,NULL=长期有效",
|
||
)
|
||
deleted_at: Mapped[str | None] = mapped_column(
|
||
String(32),
|
||
nullable=True,
|
||
comment="进入回收站时间(NULL=未删除),3 天后自动清理",
|
||
)
|
||
|
||
# 关系
|
||
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})>"
|