Skip to content

Commit

Permalink
fix mysql migration
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnider2195 committed Jan 7, 2025
1 parent 615f7cf commit 1e4852b
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
schema_editor.connection.cursor(), through_model_db_table
)
for constraint_name, constraint in through_model_db_table_constraints.items():
# Skip primary key constraints
if constraint.get("primary_key", False):
continue

# Change index names
if constraint.get("index", False):
new_index_name = schema_editor._create_index_name(through_model_db_table, constraint["columns"])
# This is ANOTHER workaround since Django does not render the indexes for an auto-created M2M table. We SHOULD be able to use schema_editor.rename_index() but it doesn't work.
Expand All @@ -38,6 +43,13 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
params=None,
)

# Change foreign key constraint names
if constraint.get("foreign_key", None):
constraint_suffix = "_fk_%(to_table)s_%(to_column)s"
field = through_model._meta.get_field(constraint["columns"][0])
schema_editor.execute(schema_editor._delete_fk_sql(through_model, constraint_name))
schema_editor.execute(schema_editor._create_fk_sql(through_model, field, constraint_suffix))

def database_backwards(self, app_label, schema_editor, from_state, to_state):
"""
Since this runs separately from the M2M field rename operation, we can't actually reverse this operation.
Expand Down

0 comments on commit 1e4852b

Please sign in to comment.