45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
"""语义事实读模型。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import BigInteger, Boolean, Float, Integer, Text, UniqueConstraint, VARCHAR
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from .base import Base
|
|
|
|
|
|
class SemanticFact(Base):
|
|
__tablename__ = "semantic_facts"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
entity_type: Mapped[str] = mapped_column(VARCHAR(32), nullable=False, index=True)
|
|
entity_id: Mapped[int] = mapped_column(BigInteger, nullable=False, index=True)
|
|
fact_namespace: Mapped[str] = mapped_column(VARCHAR(64), nullable=False, index=True)
|
|
fact_key: Mapped[str] = mapped_column(VARCHAR(64), nullable=False, index=True)
|
|
fact_value_text: Mapped[str] = mapped_column(Text, nullable=False)
|
|
fact_value_norm: Mapped[str] = mapped_column(VARCHAR(255), nullable=False, index=True)
|
|
value_type: Mapped[str] = mapped_column(VARCHAR(32), nullable=False, default="keyword")
|
|
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
|
evidence_source_type: Mapped[Optional[str]] = mapped_column(VARCHAR(32), nullable=True)
|
|
evidence_source_id: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
|
|
evidence_excerpt: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
extract_version: Mapped[Optional[str]] = mapped_column(VARCHAR(64), nullable=True)
|
|
freshness_at: Mapped[Optional[str]] = mapped_column(VARCHAR(64), nullable=True)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, index=True)
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
"tenant_id",
|
|
"entity_type",
|
|
"entity_id",
|
|
"fact_key",
|
|
"fact_value_norm",
|
|
name="uq_semantic_facts_tenant_entity_key_norm",
|
|
),
|
|
)
|
|
|
|
|
|
__all__ = ["SemanticFact"]
|