diff options
| author | David Wobrock <david.wobrock@gmail.com> | 2023-12-22 09:49:56 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-12-22 12:47:17 +0100 |
| commit | 70456610694d24d07d76c64fd3bc40f664be51ea (patch) | |
| tree | 2aaafa784b7e0577930c87e19c136af7cd6b7dd4 | |
| parent | 6c08dba5176606e8a62dd108bb6c9467d678d2f4 (diff) | |
Fixed #35022 -- Fixed RenameIndex() crash on unnamed indexes if exists unique constraint on the same fields.
| -rw-r--r-- | django/db/migrations/operations/models.py | 5 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 20 |
2 files changed, 24 insertions, 1 deletions
diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index d616cafb45..df153da7e5 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -1017,7 +1017,10 @@ class RenameIndex(IndexOperation): 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( diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 7643111c34..2d9d3a38f0 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -3626,6 +3626,26 @@ class OperationTests(OperationTestBase): 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") |
