diff --git a/netbox/dcim/migrations/0228_rack_group.py b/netbox/dcim/migrations/0228_rack_group.py index 160961847..27ffd97d7 100644 --- a/netbox/dcim/migrations/0228_rack_group.py +++ b/netbox/dcim/migrations/0228_rack_group.py @@ -14,6 +14,58 @@ class Migration(migrations.Migration): ] operations = [ + # Rename legacy database objects left over from when the RackGroup model was renamed + # to Location (v2.11) and Rack.group was renamed to Rack.location. Old installations + # retained the original names, which conflict with the new dcim_rackgroup table and + # dcim_rack.group_id column created by this migration. No-op on fresh installs. + migrations.RunSQL( + sql=[ + "ALTER INDEX IF EXISTS dcim_rackgroup_pkey RENAME TO dcim_location_pkey", + "ALTER INDEX IF EXISTS dcim_rackgroup_parent_id_cc315105 RENAME TO dcim_location_parent_id_d77f3318", + "ALTER INDEX IF EXISTS dcim_rackgroup_site_id_13520e89 RENAME TO dcim_location_site_id_b55e975f", + "ALTER INDEX IF EXISTS dcim_rackgroup_slug_3f4582a7 RENAME TO dcim_location_slug_352c5472", + "ALTER INDEX IF EXISTS dcim_rackgroup_slug_3f4582a7_like RENAME TO dcim_location_slug_352c5472_like", + "ALTER INDEX IF EXISTS dcim_rackgroup_tree_id_9c2ad6f4 RENAME TO dcim_location_tree_id_5089ef14", + "ALTER SEQUENCE IF EXISTS dcim_rackgroup_id_seq RENAME TO dcim_location_id_seq", + # Rename the legacy index on dcim_rack from when Rack.group was renamed to + # Rack.location. The column was renamed but the index was not, so it still + # carries the old "group_id" name while indexing location_id. Its name + # collides with the new index Django creates for the new Rack.group FK. + "ALTER INDEX IF EXISTS dcim_rack_group_id_44e90ea9 RENAME TO dcim_rack_location_id_5f63ec31", + ], + reverse_sql=migrations.RunSQL.noop, + ), + # PostgreSQL does not support IF EXISTS on RENAME CONSTRAINT, so use a DO block. + # Target names match what a fresh v4.5.9 install produces (Django generates the FK + # constraint name as