"""河图世界域表模型:境界(persistent_worlds)与世界认知实体/关系。 设计依据 core-domains.md §4 三界与 data-model.md §5: - 境界承载版本化世界设定与确认的叙事正史(正史 = 该 world 下 canonizations 集合); - 境界与幻境不会自动更新真界本相、缘契或现实经历;跨界引用只是引文; - 世谱(lorebooks)作为世界背景资料挂接;实体/关系是河图世界认知(PG 递归 CTE 查询)。 """ from datetime import datetime from sqlalchemy import DateTime, ForeignKey, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.core.ids import new_id from app.core.time import utc_now from app.db.base import Base class PersistentWorld(Base): """境界:世界背景、规则与世谱挂接;正史版本随 canonizations 演进。""" __tablename__ = "persistent_worlds" id: Mapped[str] = mapped_column(String(80), primary_key=True, default=lambda: new_id("world")) owner_scope: Mapped[str] = mapped_column(String(80), nullable=False, index=True) name: Mapped[str] = mapped_column(String(120), nullable=False) description: Mapped[str] = mapped_column(Text, nullable=False, default="") # 世界规则与设定(自由结构;组装境界运行时上下文) rules_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}") # 世谱挂接(lorebook id;世界背景资料经世谱激活管线进入投影) lorebook_id: Mapped[str | None] = mapped_column(String(80), index=True) # 正史版本号:该世界下 canonization 计数(视图派生,此列为缓存计数) canon_count: Mapped[int] = mapped_column(nullable=False, default=0) status: Mapped[str] = mapped_column(String(20), nullable=False, default="active") created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, onupdate=utc_now) class WorldEntity(Base): """河图世界实体:境界内的人物/地点/组织/物品认知(世界作用域)。""" __tablename__ = "world_entities" id: Mapped[str] = mapped_column(String(80), primary_key=True, default=lambda: new_id("wentity")) world_id: Mapped[str] = mapped_column( String(80), ForeignKey("persistent_worlds.id"), nullable=False, index=True ) # character / location / organization / item / concept(河图实体类型) kind: Mapped[str] = mapped_column(String(30), nullable=False, default="concept") name: Mapped[str] = mapped_column(String(120), nullable=False) description: Mapped[str] = mapped_column(Text, nullable=False, default="") # 属性与不确定性(河图:状态与不确定性认知) attrs_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}") created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now) class WorldRelation(Base): """河图世界关系:实体间有向关系(图查询走 PG 递归 CTE)。""" __tablename__ = "world_relations" id: Mapped[str] = mapped_column(String(80), primary_key=True, default=lambda: new_id("wrel")) world_id: Mapped[str] = mapped_column( String(80), ForeignKey("persistent_worlds.id"), nullable=False, index=True ) source_entity_id: Mapped[str] = mapped_column( String(80), ForeignKey("world_entities.id"), nullable=False, index=True ) target_entity_id: Mapped[str] = mapped_column( String(80), ForeignKey("world_entities.id"), nullable=False, index=True ) relation: Mapped[str] = mapped_column(String(80), nullable=False) note: Mapped[str] = mapped_column(Text, nullable=False, default="") created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)