@@ -542,7 +542,7 @@ class Meta:
542542 new_field = EmbeddedModelField (Author )
543543 new_field .set_attributes_from_name ("author" )
544544 with connection .schema_editor () as editor :
545- # Create the table amd add the field.
545+ # Create the table and add the field.
546546 editor .create_model (Book )
547547 editor .add_field (Book , new_field )
548548 # Embedded uniques are created.
@@ -582,40 +582,123 @@ class Meta:
582582 self .assertTableNotExists (Book )
583583
584584 @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" ])]
587602
588603 class Book (models .Model ):
589- name = models . CharField ( max_length = 100 )
604+ author = EmbeddedModelField ( Author )
590605
591606 class Meta :
592607 app_label = "schema_"
593608
594609 new_field = EmbeddedModelField (Author )
595610 new_field .set_attributes_from_name ("author" )
596-
597611 with connection .schema_editor () as editor :
598- # Create the table amd add the field.
612+ # Create the table and add the field.
599613 editor .create_model (Book )
600614 editor .add_field (Book , new_field )
601615 # Embedded indexes are created.
602616 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 " ],
605619 )
606620 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" ],
609626 )
610627 editor .remove_field (Book , new_field )
611628 # Embedded indexes are removed.
612629 self .assertEqual (
613- self .get_constraints_for_columns (Book , ["author.age " ]),
630+ self .get_constraints_for_columns (Book , ["author.indexed_two " ]),
614631 [],
615632 )
616633 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+ ),
618638 [],
619639 )
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 )
621704 self .assertTableNotExists (Author )
0 commit comments