Skip to content

Commit 0154965

Browse files
committed
Fixed #23013 -- Fixed removing unique_together/index_together constraints in migrations.
Thanks melinath for the report.
1 parent b65a200 commit 0154965

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

django/db/migrations/operations/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def references_model(self, name, app_label=None):
248248
return name.lower() == self.name.lower()
249249

250250
def describe(self):
251-
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.unique_together))
251+
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.unique_together or ''))
252252

253253

254254
class AlterIndexTogether(Operation):
@@ -288,7 +288,7 @@ def references_model(self, name, app_label=None):
288288
return name.lower() == self.name.lower()
289289

290290
def describe(self):
291-
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.index_together))
291+
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.index_together or ''))
292292

293293

294294
class AlterOrderWithRespectTo(Operation):

django/db/migrations/state.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ def render(self, apps):
272272
# First, make a Meta object
273273
meta_contents = {'app_label': self.app_label, "apps": apps}
274274
meta_contents.update(self.options)
275-
if "unique_together" in meta_contents:
276-
meta_contents["unique_together"] = list(meta_contents["unique_together"])
277275
meta = type(str("Meta"), tuple(), meta_contents)
278276
# Then, work out our bases
279277
try:

tests/migrations/test_operations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ def test_alter_unique_together(self):
899899
operation.state_forwards("test_alunto", new_state)
900900
self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
901901

902+
def test_alter_unique_together_remove(self):
903+
operation = migrations.AlterUniqueTogether("Pony", None)
904+
self.assertEqual(operation.describe(), "Alter unique_together for Pony (0 constraint(s))")
905+
902906
def test_alter_index_together(self):
903907
"""
904908
Tests the AlterIndexTogether operation.
@@ -922,6 +926,10 @@ def test_alter_index_together(self):
922926
operation.database_backwards("test_alinto", editor, new_state, project_state)
923927
self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
924928

929+
def test_alter_index_together_remove(self):
930+
operation = migrations.AlterIndexTogether("Pony", None)
931+
self.assertEqual(operation.describe(), "Alter index_together for Pony (0 constraint(s))")
932+
925933
def test_alter_model_options(self):
926934
"""
927935
Tests the AlterModelOptions operation.

0 commit comments

Comments
 (0)