Skip to content

Commit

Permalink
Fixed #35022 -- Fixed RenameIndex() crash on unnamed indexes if exist…
Browse files Browse the repository at this point in the history
…s unique constraint on the same fields.
  • Loading branch information
David-Wobrock authored and felixxm committed Dec 22, 2023
1 parent 6c08dba commit 7045661
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django/db/migrations/operations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
from_model._meta.get_field(field).column for field in self.old_fields
]
matching_index_name = schema_editor._constraint_names(
from_model, column_names=columns, index=True
from_model,
column_names=columns,
index=True,
unique=False,
)
if len(matching_index_name) != 1:
raise ValueError(
Expand Down
20 changes: 20 additions & 0 deletions tests/migrations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3626,6 +3626,26 @@ def test_rename_index_unknown_unnamed_index(self):
with self.assertRaisesMessage(ValueError, msg):
operation.database_forwards(app_label, editor, project_state, new_state)

@skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
def test_rename_index_unnamed_index_with_unique_index(self):
app_label = "test_rninuniwui"
project_state = self.set_up_test_model(
app_label,
multicol_index=True,
unique_together=True,
)
table_name = app_label + "_pony"
self.assertIndexNotExists(table_name, "new_pony_test_idx")
operation = migrations.RenameIndex(
"Pony", new_name="new_pony_test_idx", old_fields=["pink", "weight"]
)
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
# Rename index.
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
self.assertIndexNameExists(table_name, "new_pony_test_idx")

def test_add_index_state_forwards(self):
project_state = self.set_up_test_model("test_adinsf")
index = models.Index(fields=["pink"], name="test_adinsf_pony_pink_idx")
Expand Down

0 comments on commit 7045661

Please sign in to comment.