From 3d4d0205b07e7a3f263fd86172c29351182accf5 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 26 Nov 2024 11:25:12 -0500 Subject: [PATCH] add unique_together --- django_mongodb/schema.py | 2 + tests/schema_/test_embedded_model.py | 73 +++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/django_mongodb/schema.py b/django_mongodb/schema.py index 4f76c387..7c61279c 100644 --- a/django_mongodb/schema.py +++ b/django_mongodb/schema.py @@ -235,6 +235,8 @@ def alter_unique_together( model, field_names, {"unique": True, "primary_key": False}, + column_prefix=column_prefix, + parent_model=parent_model, ) # Created uniques for field_names in news.difference(olds): diff --git a/tests/schema_/test_embedded_model.py b/tests/schema_/test_embedded_model.py index b934bd8a..f51c01e0 100644 --- a/tests/schema_/test_embedded_model.py +++ b/tests/schema_/test_embedded_model.py @@ -395,7 +395,7 @@ class Meta: # SchemaEditor.add_field() / remove_field() tests @isolate_apps("schema_") def test_add_remove_field_db_index_and_unique(self): - """AddField + EmbeddedModelField""" + """AddField/RemoveField + EmbeddedModelField + Field(db_index=True) & Field(unique=True).""" class Book(models.Model): name = models.CharField(max_length=100) @@ -451,7 +451,7 @@ class Meta: @ignore_warnings(category=RemovedInDjango51Warning) @isolate_apps("schema_") def test_add_remove_field_index_together(self): - """Meta.index_together on an embedded model.""" + """AddField/RemoveField + EmbeddedModelField + Meta.index_together.""" class Address(models.Model): index_together_one = models.CharField(max_length=10) @@ -512,6 +512,75 @@ class Meta: editor.delete_model(Book) self.assertTableNotExists(Book) + @isolate_apps("schema_") + def test_add_remove_field_unique_together(self): + """AddField/RemoveField + EmbeddedModelField + Meta.unique_together.""" + + class Address(models.Model): + unique_together_one = models.CharField(max_length=10) + unique_together_two = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + unique_together = [("unique_together_one", "unique_together_two")] + + class Author(models.Model): + address = EmbeddedModelField(Address) + unique_together_three = models.CharField(max_length=10) + unique_together_four = models.CharField(max_length=10) + + class Meta: + app_label = "schema_" + unique_together = [("unique_together_three", "unique_together_four")] + + class Book(models.Model): + author = EmbeddedModelField(Author) + + class Meta: + app_label = "schema_" + + new_field = EmbeddedModelField(Author) + new_field.set_attributes_from_name("author") + with connection.schema_editor() as editor: + # Create the table amd add the field. + editor.create_model(Book) + editor.add_field(Book, new_field) + # Embedded uniques are created. + self.assertEqual( + self.get_constraints_for_columns( + Book, ["author.unique_together_three", "author.unique_together_four"] + ), + [ + "schema__author_author.unique_together_three_author.unique_together_four_39e1cb43_uniq" + ], + ) + self.assertEqual( + self.get_constraints_for_columns( + Book, + ["author.address.unique_together_one", "author.address.unique_together_two"], + ), + [ + "schema__address_author.address.unique_together_one_author.address.unique_together_two_de682e30_uniq" + ], + ) + editor.remove_field(Book, new_field) + # Embedded indexes are removed. + self.assertEqual( + self.get_constraints_for_columns( + Book, ["author.unique_together_three", "author.unique_together_four"] + ), + [], + ) + self.assertEqual( + self.get_constraints_for_columns( + Book, + ["author.address.unique_together_one", "author.address.unique_together_two"], + ), + [], + ) + editor.delete_model(Book) + self.assertTableNotExists(Book) + @isolate_apps("schema_") def _test_add_field_db_index(self): """AddField + EmbeddedModelField"""