@@ -58,14 +58,80 @@ def rename_column_in_all_schemas(apps, schema_editor):
5858 has_tag_id = cursor .fetchone ()[0 ]
5959
6060 if has_taxontag_id and not has_tag_id :
61- print (f" Renaming column in { schema_name } ..." )
61+ print (f" Updating { schema_name } ..." )
62+
63+ # Drop old foreign key constraint to bims_taxontag if it exists
64+ cursor .execute (f"""
65+ SELECT constraint_name
66+ FROM information_schema.table_constraints
67+ WHERE table_schema = '{ schema_name } '
68+ AND table_name = 'bims_taggroup_tags'
69+ AND constraint_type = 'FOREIGN KEY'
70+ AND constraint_name LIKE '%taxontag%'
71+ """ )
72+ old_constraint = cursor .fetchone ()
73+ if old_constraint :
74+ constraint_name = old_constraint [0 ]
75+ print (f" Dropping old FK constraint: { constraint_name } " )
76+ cursor .execute (f"""
77+ ALTER TABLE { schema_name } .bims_taggroup_tags
78+ DROP CONSTRAINT { constraint_name }
79+ """ )
80+
81+ # Rename column
6282 cursor .execute (f"""
6383 ALTER TABLE { schema_name } .bims_taggroup_tags
6484 RENAME COLUMN taxontag_id TO tag_id
6585 """ )
66- print (f" ✓ Renamed taxontag_id to tag_id in { schema_name } " )
86+
87+ # Add new foreign key constraint to taggit_tag
88+ print (f" Adding new FK constraint to taggit_tag" )
89+ cursor .execute (f"""
90+ ALTER TABLE { schema_name } .bims_taggroup_tags
91+ ADD CONSTRAINT bims_taggroup_tags_tag_id_fkey
92+ FOREIGN KEY (tag_id) REFERENCES { schema_name } .taggit_tag(id)
93+ ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
94+ """ )
95+
96+ print (f" ✓ Updated { schema_name } " )
6797 elif has_tag_id :
6898 print (f" Skipping { schema_name } : tag_id column already exists" )
99+ # Check if the correct FK constraint exists
100+ cursor .execute (f"""
101+ SELECT constraint_name
102+ FROM information_schema.table_constraints
103+ WHERE table_schema = '{ schema_name } '
104+ AND table_name = 'bims_taggroup_tags'
105+ AND constraint_type = 'FOREIGN KEY'
106+ AND constraint_name LIKE '%tag_id%'
107+ """ )
108+ fk_exists = cursor .fetchone ()
109+
110+ if not fk_exists :
111+ # Need to add the FK constraint even though column exists
112+ print (f" Adding missing FK constraint to taggit_tag" )
113+ # First drop any old constraint
114+ cursor .execute (f"""
115+ SELECT constraint_name
116+ FROM information_schema.table_constraints
117+ WHERE table_schema = '{ schema_name } '
118+ AND table_name = 'bims_taggroup_tags'
119+ AND constraint_type = 'FOREIGN KEY'
120+ AND constraint_name LIKE '%taxontag%'
121+ """ )
122+ old_constraint = cursor .fetchone ()
123+ if old_constraint :
124+ cursor .execute (f"""
125+ ALTER TABLE { schema_name } .bims_taggroup_tags
126+ DROP CONSTRAINT { old_constraint [0 ]}
127+ """ )
128+
129+ cursor .execute (f"""
130+ ALTER TABLE { schema_name } .bims_taggroup_tags
131+ ADD CONSTRAINT bims_taggroup_tags_tag_id_fkey
132+ FOREIGN KEY (tag_id) REFERENCES { schema_name } .taggit_tag(id)
133+ ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
134+ """ )
69135 else :
70136 print (f" Skipping { schema_name } : taxontag_id column doesn't exist" )
71137
0 commit comments