"""任务模型 — 支持无限嵌套""" import uuid from datetime import datetime, timezone from sqlalchemy import String, DateTime, Boolean, ForeignKey from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base class Task(Base): __tablename__ = "tasks" id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, ) # ---- 物理外键(关联本库 products) ---- product_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("products.id"), nullable=False, comment="所属产品ID", ) # ---- 物理外键(自引用:无限嵌套父子任务) ---- parent_task_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("tasks.id"), nullable=True, index=True, comment="父任务ID", ) task_name: Mapped[str] = mapped_column( String(200), nullable=False, comment="任务名称", ) # ---- 逻辑外键(关联老系统用户表,仅存储 ID,无物理约束) ---- assignee_id: Mapped[str | None] = mapped_column( String(64), nullable=True, comment="负责人ID(逻辑外键→老系统)", ) status: Mapped[str] = mapped_column( String(50), nullable=False, default="pending", comment="任务状态", ) notify_parent_on_complete: Mapped[bool] = mapped_column( Boolean, default=False, comment="完成后是否通知父任务", ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), comment="创建时间", ) # ---- 关系 ---- product: Mapped["Product"] = relationship("Product", lazy="selectin") parent_task: Mapped["Task | None"] = relationship( "Task", remote_side="Task.id", back_populates="child_tasks", lazy="selectin", ) child_tasks: Mapped[list["Task"]] = relationship( "Task", back_populates="parent_task", lazy="selectin", ) def __repr__(self) -> str: return f""