Skip to content

Commit

Permalink
add support for Meta.indexes on embedded models
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Nov 21, 2024
1 parent b8c2100 commit 0db42ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_mongodb/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/schema_/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]


Expand All @@ -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")]


Expand Down
20 changes: 20 additions & 0 deletions tests/schema_/test_embedded_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0db42ed

Please sign in to comment.