61 lines
3.1 KiB
Python
61 lines
3.1 KiB
Python
"""表元数据模型"""
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from sqlalchemy import Integer, String, Text, DateTime, Boolean, JSON
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
from ..db.base import BaseModel
|
|
|
|
class TableMetadata(BaseModel):
|
|
"""表元数据表"""
|
|
__tablename__ = "table_metadata"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
# database_config_id = Column(Integer, ForeignKey('database_configs.id'), nullable=False)
|
|
table_name: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
|
table_schema: Mapped[str] = mapped_column(String(50), default='public')
|
|
table_type: Mapped[str] = mapped_column(String(20), default='BASE TABLE')
|
|
table_comment: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # 表描述
|
|
database_config_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) #数据库配置ID
|
|
# 表结构信息
|
|
columns_info: Mapped[dict] = mapped_column(JSON, nullable=False) # 列信息:名称、类型、注释等
|
|
primary_keys: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) # 主键列表
|
|
foreign_keys: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) # 外键信息
|
|
indexes: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) # 索引信息
|
|
|
|
# 示例数据
|
|
sample_data: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) # 前5条示例数据
|
|
row_count: Mapped[int] = mapped_column(Integer, default=0) # 总行数
|
|
|
|
# 问答相关
|
|
is_enabled_for_qa: Mapped[bool] = mapped_column(Boolean, default=True) # 是否启用问答
|
|
qa_description: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # 问答描述
|
|
business_context: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # 业务上下文
|
|
|
|
last_synced_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True) # 最后同步时间
|
|
|
|
# 关系
|
|
# database_config = relationship("DatabaseConfig", back_populates="table_metadata")
|
|
|
|
def to_dict(self):
|
|
return {
|
|
"id": self.id,
|
|
"created_by": self.created_by, # 改为created_by
|
|
"database_config_id": self.database_config_id,
|
|
"table_name": self.table_name,
|
|
"table_schema": self.table_schema,
|
|
"table_type": self.table_type,
|
|
"table_comment": self.table_comment,
|
|
"columns_info": self.columns_info,
|
|
"primary_keys": self.primary_keys,
|
|
# "foreign_keys": self.foreign_keys,
|
|
"indexes": self.indexes,
|
|
"sample_data": self.sample_data,
|
|
"row_count": self.row_count,
|
|
"is_enabled_for_qa": self.is_enabled_for_qa,
|
|
"qa_description": self.qa_description,
|
|
"business_context": self.business_context,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
|
"last_synced_at": self.last_synced_at.isoformat() if self.last_synced_at else None
|
|
} |