from datetime import datetime from sqlalchemy import Boolean, DateTime, Index, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class Category(Base): __tablename__ = "category" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) name: Mapped[str] = mapped_column(String(30), nullable=False, comment="分类名称") parent_id: Mapped[int | None] = mapped_column(Integer, comment="父分类ID") sort_order: Mapped[int] = mapped_column(Integer, default=0, comment="排序") is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否启用") created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) __table_args__ = ( # 分类树查询:活跃 + 排序 Index("ix_category_active_sort", "is_active", "sort_order"), ) class CategoryMapping(Base): __tablename__ = "category_mapping" id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) category_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="统一分类ID") source_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="资源站ID") source_type_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="源站分类ID") source_type_name: Mapped[str] = mapped_column(String(30), nullable=False, comment="源站分类名称") __table_args__ = ( # 采集器分类映射查找:source_id + source_type_id 定位分类 Index("ix_category_mapping_source_type", "source_id", "source_type_id"), )