@@ -542,7 +542,7 @@ class Meta:
542
542
new_field = EmbeddedModelField (Author )
543
543
new_field .set_attributes_from_name ("author" )
544
544
with connection .schema_editor () as editor :
545
- # Create the table amd add the field.
545
+ # Create the table and add the field.
546
546
editor .create_model (Book )
547
547
editor .add_field (Book , new_field )
548
548
# Embedded uniques are created.
@@ -582,40 +582,123 @@ class Meta:
582
582
self .assertTableNotExists (Book )
583
583
584
584
@isolate_apps ("schema_" )
585
- def _test_add_field_db_index (self ):
586
- """AddField + EmbeddedModelField"""
585
+ def test_add_remove_field_indexes (self ):
586
+ """AddField/RemoveField + EmbeddedModelField + Meta.indexes."""
587
+
588
+ class Address (models .Model ):
589
+ indexed_one = models .CharField (max_length = 10 )
590
+
591
+ class Meta :
592
+ app_label = "schema_"
593
+ indexes = [models .Index (fields = ["indexed_one" ])]
594
+
595
+ class Author (models .Model ):
596
+ address = EmbeddedModelField (Address )
597
+ indexed_two = models .CharField (max_length = 10 )
598
+
599
+ class Meta :
600
+ app_label = "schema_"
601
+ indexes = [models .Index (fields = ["indexed_two" ])]
587
602
588
603
class Book (models .Model ):
589
- name = models . CharField ( max_length = 100 )
604
+ author = EmbeddedModelField ( Author )
590
605
591
606
class Meta :
592
607
app_label = "schema_"
593
608
594
609
new_field = EmbeddedModelField (Author )
595
610
new_field .set_attributes_from_name ("author" )
596
-
597
611
with connection .schema_editor () as editor :
598
- # Create the table amd add the field.
612
+ # Create the table and add the field.
599
613
editor .create_model (Book )
600
614
editor .add_field (Book , new_field )
601
615
# Embedded indexes are created.
602
616
self .assertEqual (
603
- self .get_constraints_for_columns (Book , ["author.age " ]),
604
- ["schema__book_author.age_dc08100b " ],
617
+ self .get_constraints_for_columns (Book , ["author.indexed_two " ]),
618
+ ["schema__aut_indexed_b19137_idx " ],
605
619
)
606
620
self .assertEqual (
607
- self .get_constraints_for_columns (Book , ["author.address.zip_code" ]),
608
- ["schema__book_author.address.zip_code_7b9a9307" ],
621
+ self .get_constraints_for_columns (
622
+ Book ,
623
+ ["author.address.indexed_one" ],
624
+ ),
625
+ ["schema__add_indexed_b64972_idx" ],
609
626
)
610
627
editor .remove_field (Book , new_field )
611
628
# Embedded indexes are removed.
612
629
self .assertEqual (
613
- self .get_constraints_for_columns (Book , ["author.age " ]),
630
+ self .get_constraints_for_columns (Book , ["author.indexed_two " ]),
614
631
[],
615
632
)
616
633
self .assertEqual (
617
- self .get_constraints_for_columns (Book , ["author.address.zip_code" ]),
634
+ self .get_constraints_for_columns (
635
+ Book ,
636
+ ["author.address.indexed_one" ],
637
+ ),
618
638
[],
619
639
)
620
- editor .delete_model (Book )
640
+ editor .delete_model (Author )
641
+ self .assertTableNotExists (Author )
642
+
643
+ @isolate_apps ("schema_" )
644
+ def test_add_remove_field_constraints (self ):
645
+ """AddField/RemoveField + EmbeddedModelField + Meta.constraints."""
646
+
647
+ class Address (models .Model ):
648
+ unique_constraint_one = models .CharField (max_length = 10 )
649
+
650
+ class Meta :
651
+ app_label = "schema_"
652
+ constraints = [
653
+ models .UniqueConstraint (fields = ["unique_constraint_one" ], name = "unique_one" )
654
+ ]
655
+
656
+ class Author (models .Model ):
657
+ address = EmbeddedModelField (Address )
658
+ unique_constraint_two = models .CharField (max_length = 10 )
659
+
660
+ class Meta :
661
+ app_label = "schema_"
662
+ constraints = [
663
+ models .UniqueConstraint (fields = ["unique_constraint_two" ], name = "unique_two" )
664
+ ]
665
+
666
+ class Book (models .Model ):
667
+ author = EmbeddedModelField (Author )
668
+
669
+ class Meta :
670
+ app_label = "schema_"
671
+
672
+ new_field = EmbeddedModelField (Author )
673
+ new_field .set_attributes_from_name ("author" )
674
+ with connection .schema_editor () as editor :
675
+ # Create the table and add the field.
676
+ editor .create_model (Book )
677
+ editor .add_field (Book , new_field )
678
+ # Embedded constraints are created.
679
+ self .assertEqual (
680
+ self .get_constraints_for_columns (Book , ["author.unique_constraint_two" ]),
681
+ ["unique_two" ],
682
+ )
683
+ self .assertEqual (
684
+ self .get_constraints_for_columns (
685
+ Book ,
686
+ ["author.address.unique_constraint_one" ],
687
+ ),
688
+ ["unique_one" ],
689
+ )
690
+ editor .remove_field (Book , new_field )
691
+ # Embedded constraints are removed.
692
+ self .assertEqual (
693
+ self .get_constraints_for_columns (Book , ["author.unique_constraint_two" ]),
694
+ [],
695
+ )
696
+ self .assertEqual (
697
+ self .get_constraints_for_columns (
698
+ Book ,
699
+ ["author.address.unique_constraint_one" ],
700
+ ),
701
+ [],
702
+ )
703
+ editor .delete_model (Author )
621
704
self .assertTableNotExists (Author )
0 commit comments