-- ============================================================ -- 断链焊接:系谱锚点 + 遗传力落库 -- 幂等:全部 IF NOT EXISTS。 -- 1) bre_tree 加 母本 dam_id / 父本 sire_id(FK→bre_germplasm) -- —— A 矩阵的系谱锚点(单株为子代、种质为亲本) -- 2) 按杂交组合回填现有单株的 dam/sire(组合无亲本的保持 NULL → founder) -- 3) bre_prediction 加 遗传力 heritability(§3.12) -- 注:SQLAlchemy create_all 只建新表、不改已有表,故已有库需执行本脚本一次。 -- ============================================================ -- ── 1) bre_tree:母本/父本 ────────────────────────────── ALTER TABLE bre_tree ADD COLUMN IF NOT EXISTS dam_id INTEGER REFERENCES bre_germplasm(id) ON DELETE SET NULL; ALTER TABLE bre_tree ADD COLUMN IF NOT EXISTS sire_id INTEGER REFERENCES bre_germplasm(id) ON DELETE SET NULL; CREATE INDEX IF NOT EXISTS ix_bre_tree_dam ON bre_tree(dam_id); CREATE INDEX IF NOT EXISTS ix_bre_tree_sire ON bre_tree(sire_id); -- ── 2) 回填现有单株(组合 female/male 亲本 → 树 dam/sire)──── -- 重复执行安全:再次回填与组合同步(覆盖早期手工/旧值)。 UPDATE bre_tree t SET dam_id = c.female_parent_id, sire_id = c.male_parent_id FROM bre_cross_combination c WHERE t.combination_id = c.id AND t.is_deleted = false; -- ── 3) bre_prediction:遗传力 h² ───────────────────────── ALTER TABLE bre_prediction ADD COLUMN IF NOT EXISTS heritability NUMERIC;