@@ -163,7 +163,7 @@ def assertTableExists(self, model):
163
163
def assertTableNotExists (self , model ):
164
164
self .assertNotIn (model ._meta .db_table , connection .introspection .table_names ())
165
165
166
- # Tests
166
+ # SchemaEditor.create_model() tests
167
167
def test_db_index (self ):
168
168
"""Field(db_index=True) on an embedded model."""
169
169
with connection .schema_editor () as editor :
@@ -391,3 +391,33 @@ class Meta:
391
391
)
392
392
editor .delete_model (Author )
393
393
self .assertTableNotExists (Author )
394
+
395
+ # SchemaEditor.add_field() tests
396
+ @isolate_apps ("schema_" )
397
+ def test_add_field_db_index (self ):
398
+ """AddField + EmbeddedModelField"""
399
+
400
+ class Book (models .Model ):
401
+ name = models .CharField (max_length = 100 )
402
+
403
+ class Meta :
404
+ app_label = "schema_"
405
+
406
+ new_field = EmbeddedModelField (Author )
407
+ new_field .set_attributes_from_name ("author" )
408
+
409
+ with connection .schema_editor () as editor :
410
+ # Create the table amd add the field.
411
+ editor .create_model (Book )
412
+ editor .add_field (Book , new_field )
413
+ # Embedded indexes are created.
414
+ self .assertEqual (
415
+ self .get_constraints_for_columns (Book , ["author.age" ]),
416
+ ["schema__book_author.age_dc08100b" ],
417
+ )
418
+ self .assertEqual (
419
+ self .get_constraints_for_columns (Book , ["author.address.zip_code" ]),
420
+ ["schema__book_author.address.zip_code_7b9a9307" ],
421
+ )
422
+ editor .delete_model (Book )
423
+ self .assertTableNotExists (Author )
0 commit comments