Changes between Version 1 and Version 2 of Ticket #34647


Ignore:
Timestamp:
Jun 9, 2023, 7:17:21 AM (19 months ago)
Author:
Steven Mapes
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34647

    • Property Summary Foreign Key index names are not renamed when a model is renamedForeign Key index names are not renamed when a model is renamed causing duplicate key if a model with the origin name and column is added
  • Ticket #34647 – Description

    v1 v2  
    33The way I encountered this was to take a model called {{{CriticalPathTemplate}}} with a property called {{{archived_by}}} which is a foreign key to the user table, then renamed the model to {{{CriticalPathTemplateOld}}} and ran migrations. This renamed the table but left the indexes as they were.
    44
    5 I then added a new model called {{{CriticalPathTemplate}}} (business requirement) and ran makemigrations and migrate. The migrations were made but makemigrations failed due to a foreign key name clash. I ended up renaming the column but then hit further issues for the next FK in the table E.G {{{django.db.utils.OperationalError: (1826, "Duplicate foreign key constraint name 'critical_path_templa_origin_agent_shortco_f9715e87_fk_address_s'")
    6 }}}
     5I then added a new model called {{{CriticalPathTemplate}}}, a business requirement, and ran makemigrations and migrate. The migrations were made but makemigrations failed due to a foreign key name clash. I ended up renaming the column but then hit further issues for the next FK in the table, there are a few.
    76
    87Looking at the output of sqlmigrate for the new migration and comparing the name of the foreign keys that Django generated I can see several that are going to clash as the indexes on the old table were not renamed when the table was renamed by the migration.
     
    6665}}}
    6766
    68 run migrations, the error will occur and the DB will be left in a partially migrated state that can't be rolled back
     67run migrations, the error will occur and the DB will be left in a partially migrated state that can't be rolled back.
     68
     69It's important to do this as a three step process and not to just jump from Step 1 to Step 2 as that will result in the table being dropped and recreated which will bypass the issue but you'd loose any data that was in the table.
     70
     71It's the first time I've every had to rename a model then create a new class with the same name so I'm sure it's a rare occurrence.  My work around at the moment will be to create the migration, run a sql migrate to capture the raw SQL, then edit the migration to stop it running and create a new empty migration that can run the raw SQL but after I manually edit all the FKs that are clashing
Back to Top