Skip to content

Commit 0012479

Browse files
committed
add unique_together
1 parent 566410e commit 0012479

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

django_mongodb/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ def alter_unique_together(
235235
model,
236236
field_names,
237237
{"unique": True, "primary_key": False},
238+
column_prefix=column_prefix,
239+
parent_model=parent_model,
238240
)
239241
# Created uniques
240242
for field_names in news.difference(olds):

tests/schema_/test_embedded_model.py

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class Meta:
395395
# SchemaEditor.add_field() / remove_field() tests
396396
@isolate_apps("schema_")
397397
def test_add_remove_field_db_index_and_unique(self):
398-
"""AddField + EmbeddedModelField"""
398+
"""AddField/RemoveField + EmbeddedModelField + Field(db_index=True) & Field(unique=True)."""
399399

400400
class Book(models.Model):
401401
name = models.CharField(max_length=100)
@@ -451,7 +451,7 @@ class Meta:
451451
@ignore_warnings(category=RemovedInDjango51Warning)
452452
@isolate_apps("schema_")
453453
def test_add_remove_field_index_together(self):
454-
"""Meta.index_together on an embedded model."""
454+
"""AddField/RemoveField + EmbeddedModelField + Meta.index_together."""
455455

456456
class Address(models.Model):
457457
index_together_one = models.CharField(max_length=10)
@@ -512,6 +512,75 @@ class Meta:
512512
editor.delete_model(Book)
513513
self.assertTableNotExists(Book)
514514

515+
@isolate_apps("schema_")
516+
def test_add_remove_field_unique_together(self):
517+
"""AddField/RemoveField + EmbeddedModelField + Meta.unique_together."""
518+
519+
class Address(models.Model):
520+
unique_together_one = models.CharField(max_length=10)
521+
unique_together_two = models.CharField(max_length=10)
522+
523+
class Meta:
524+
app_label = "schema_"
525+
unique_together = [("unique_together_one", "unique_together_two")]
526+
527+
class Author(models.Model):
528+
address = EmbeddedModelField(Address)
529+
unique_together_three = models.CharField(max_length=10)
530+
unique_together_four = models.CharField(max_length=10)
531+
532+
class Meta:
533+
app_label = "schema_"
534+
unique_together = [("unique_together_three", "unique_together_four")]
535+
536+
class Book(models.Model):
537+
author = EmbeddedModelField(Author)
538+
539+
class Meta:
540+
app_label = "schema_"
541+
542+
new_field = EmbeddedModelField(Author)
543+
new_field.set_attributes_from_name("author")
544+
with connection.schema_editor() as editor:
545+
# Create the table amd add the field.
546+
editor.create_model(Book)
547+
editor.add_field(Book, new_field)
548+
# Embedded uniques are created.
549+
self.assertEqual(
550+
self.get_constraints_for_columns(
551+
Book, ["author.unique_together_three", "author.unique_together_four"]
552+
),
553+
[
554+
"schema__author_author.unique_together_three_author.unique_together_four_39e1cb43_uniq"
555+
],
556+
)
557+
self.assertEqual(
558+
self.get_constraints_for_columns(
559+
Book,
560+
["author.address.unique_together_one", "author.address.unique_together_two"],
561+
),
562+
[
563+
"schema__address_author.address.unique_together_one_author.address.unique_together_two_de682e30_uniq"
564+
],
565+
)
566+
editor.remove_field(Book, new_field)
567+
# Embedded indexes are removed.
568+
self.assertEqual(
569+
self.get_constraints_for_columns(
570+
Book, ["author.unique_together_three", "author.unique_together_four"]
571+
),
572+
[],
573+
)
574+
self.assertEqual(
575+
self.get_constraints_for_columns(
576+
Book,
577+
["author.address.unique_together_one", "author.address.unique_together_two"],
578+
),
579+
[],
580+
)
581+
editor.delete_model(Book)
582+
self.assertTableNotExists(Book)
583+
515584
@isolate_apps("schema_")
516585
def _test_add_field_db_index(self):
517586
"""AddField + EmbeddedModelField"""

0 commit comments

Comments
 (0)