"""育种单株 数据模型""" from datetime import datetime from sqlalchemy import Float, ForeignKey, Index, Integer, String, Text, text from sqlalchemy.orm import Mapped, mapped_column from app.core.base_model import MappedBase, ModelMixin, UserMixin class TreeModel(ModelMixin, UserMixin, MappedBase): """育种单株 主数据表。""" __tablename__ = "bre_tree" combination_id: Mapped[int] = mapped_column( Integer, ForeignKey("bre_cross_combination.id", ondelete="CASCADE"), index=True, nullable=False, comment="杂交组合" ) dam_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"), index=True, nullable=True, comment="母本(种质)" ) sire_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"), index=True, nullable=True, comment="父本(种质)" ) trial_study_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_trial_study.id", ondelete="SET NULL"), index=True, nullable=True, comment="所属试验/站点(MET)" ) block_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="区组号") rootstock_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_rootstock.id", ondelete="SET NULL"), index=True, nullable=True, comment="砧木" ) clone_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_clone.id", ondelete="SET NULL"), index=True, nullable=True, comment="入选克隆" ) plot_id: Mapped[int] = mapped_column( Integer, ForeignKey("bre_plot.id", ondelete="CASCADE"), index=True, nullable=True, comment="试验地块/位置" ) planting_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_planting.id", ondelete="SET NULL"), index=True, nullable=True, comment="来源定植批次(一次定植包含多棵单株)" ) tree_no: Mapped[str] = mapped_column(String(100), nullable=False, comment="单株编号") row_orientation: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="行向", default=None) row_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="行数", default=None) col_no: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="列数", default=None) planted_date: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="定植时间", default=None) bre_personnel_id: Mapped[int] = mapped_column( Integer, ForeignKey("bre_personnel.id", ondelete="CASCADE"), index=True, nullable=True, comment="责任人" ) status: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="状态", default=None) remark: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注", default=None) stage: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="当前育种阶段(breeding_stage)") generation: Mapped[str | None] = mapped_column(String(16), nullable=True, comment="世代(breeding_generation)") germplasm_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_germplasm.id", ondelete="SET NULL"), index=True, nullable=True, comment="关联种质(晋升回填,不手填)" ) entry_id: Mapped[int | None] = mapped_column( Integer, ForeignKey("bre_trial_study_entry.id", ondelete="SET NULL"), index=True, nullable=True, comment="参试条目(派生自定植)" ) # 无 status 列:覆盖基类默认 ix_<表>_status_deleted 索引, # 仅保留 (created_time, is_deleted) 复合索引用于数据权限过滤。 __table_args__ = ( Index("ix_bre_tree_created_deleted", "created_time", "is_deleted"), # 单株编号全局唯一(仅约束未软删记录,与 service 层查重语义一致) Index( "uniq_bre_tree_no", "tree_no", unique=True, postgresql_where=text("is_deleted = false"), ), ) class TreePedigreeExclusionModel(ModelMixin, UserMixin, MappedBase): """系谱隔离注册表(v2.26 N1):孟德尔检验 flag 的树断开声明亲本链(退化为 founder), 自身表型保留;后续检验不再 flag 时行被清除(可逆)。系谱消费方据此置空 dam/sire。""" __tablename__ = "bre_pedigree_exclusion" tree_id: Mapped[int] = mapped_column( Integer, ForeignKey("bre_tree.id", ondelete="CASCADE"), index=True, nullable=False, comment="单株" ) reason: Mapped[str | None] = mapped_column(String(512), nullable=True, comment="隔离原因") source_type: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="来源类型") source_check_id: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="来源检验批次ID") __table_args__ = ( # 唯一树(同树仅一条隔离记录;同树多来源时按最新覆盖) Index( "uniq_bre_pedigree_exclusion_tree", "tree_id", unique=True, postgresql_where=text("is_deleted = false"), ), Index("ix_bre_pedigree_exclusion_created_deleted", "created_time", "is_deleted"), )