diff --git a/django_mongodb/schema.py b/django_mongodb/schema.py index 05b2c756..c605620c 100644 --- a/django_mongodb/schema.py +++ b/django_mongodb/schema.py @@ -69,7 +69,7 @@ def _create_model_indexes(self, model, column_prefix="", parent_model=None): self.add_constraint(model, constraint) # Meta.indexes for index in model._meta.indexes: - self.add_index(model, index) + self.add_index(model, index, column_prefix=column_prefix, parent_model=parent_model) def delete_model(self, model): # Delete implicit M2m tables. diff --git a/tests/schema_/models.py b/tests/schema_/models.py index b16c2629..38f12482 100644 --- a/tests/schema_/models.py +++ b/tests/schema_/models.py @@ -17,9 +17,11 @@ class Address(models.Model): uid = models.IntegerField(unique=True) unique_together_one = models.CharField(max_length=10) unique_together_two = models.CharField(max_length=10) + indexed_by_index_one = models.CharField(max_length=10) class Meta: apps = new_apps + indexes = [models.Index(fields=["indexed_by_index_one"])] unique_together = [("unique_together_one", "unique_together_two")] @@ -30,9 +32,11 @@ class Author(models.Model): employee_id = models.IntegerField(unique=True) unique_together_three = models.CharField(max_length=10) unique_together_four = models.CharField(max_length=10) + indexed_by_index_two = models.CharField(max_length=10) class Meta: apps = new_apps + indexes = [models.Index(fields=["indexed_by_index_two"])] unique_together = [("unique_together_three", "unique_together_four")] diff --git a/tests/schema_/test_embedded_model.py b/tests/schema_/test_embedded_model.py index d30f1294..64799069 100644 --- a/tests/schema_/test_embedded_model.py +++ b/tests/schema_/test_embedded_model.py @@ -224,3 +224,23 @@ def test_unique_together(self): ) editor.delete_model(Author) self.assertTableNotExists(Author) + + def test_index(self): + """Meta.indexes on an embedded model.""" + with connection.schema_editor() as editor: + editor.create_model(Book) + self.assertTableExists(Book) + # Embedded uniques are created. + self.assertEqual( + self.get_constraints_for_columns(Book, ["author.indexed_by_index_two"]), + ["schema__aut_indexed_7e3a5c_idx"], + ) + self.assertEqual( + self.get_constraints_for_columns( + Book, + ["author.address.indexed_by_index_one"], + ), + ["schema__add_indexed_ef5dd6_idx"], + ) + editor.delete_model(Author) + self.assertTableNotExists(Author)