Skip to content

Commit 3fc51c6

Browse files
committed
add support for Meta.constraints and Meta.index_together
1 parent 292af3f commit 3fc51c6

File tree

3 files changed

+89
-9
lines changed

3 files changed

+89
-9
lines changed

django_mongodb_backend/schema.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def _create_model_indexes(self, model, column_prefix="", parent_model=None):
5454
self._add_field_unique(parent_model or model, field, column_prefix=column_prefix)
5555
# Meta.index_together (RemovedInDjango51Warning)
5656
for field_names in model._meta.index_together:
57-
self._add_composed_index(model, field_names)
57+
self._add_composed_index(
58+
model, field_names, column_prefix=column_prefix, parent_model=parent_model
59+
)
5860
# Meta.unique_together
5961
if model._meta.unique_together:
6062
self.alter_unique_together(
@@ -66,7 +68,9 @@ def _create_model_indexes(self, model, column_prefix="", parent_model=None):
6668
)
6769
# Meta.constraints
6870
for constraint in model._meta.constraints:
69-
self.add_constraint(model, constraint)
71+
self.add_constraint(
72+
model, constraint, column_prefix=column_prefix, parent_model=parent_model
73+
)
7074
# Meta.indexes
7175
for index in model._meta.indexes:
7276
self.add_index(model, index, column_prefix=column_prefix, parent_model=parent_model)
@@ -153,15 +157,15 @@ def remove_field(self, model, field):
153157
elif self._field_should_have_unique(field):
154158
self._remove_field_unique(model, field)
155159

156-
def alter_index_together(self, model, old_index_together, new_index_together):
160+
def alter_index_together(self, model, old_index_together, new_index_together, column_prefix=""):
157161
olds = {tuple(fields) for fields in old_index_together}
158162
news = {tuple(fields) for fields in new_index_together}
159163
# Deleted indexes
160164
for field_names in olds.difference(news):
161165
self._remove_composed_index(model, field_names, {"index": True, "unique": False})
162166
# Created indexes
163167
for field_names in news.difference(olds):
164-
self._add_composed_index(model, field_names)
168+
self._add_composed_index(model, field_names, column_prefix=column_prefix)
165169

166170
def alter_unique_together(
167171
self, model, old_unique_together, new_unique_together, column_prefix="", parent_model=None
@@ -229,11 +233,11 @@ def add_index(
229233
model = parent_model or model
230234
self.get_collection(model._meta.db_table).create_indexes([idx])
231235

232-
def _add_composed_index(self, model, field_names):
236+
def _add_composed_index(self, model, field_names, column_prefix="", parent_model=None):
233237
"""Add an index on the given list of field_names."""
234238
idx = Index(fields=field_names)
235239
idx.set_name_with_model(model)
236-
self.add_index(model, idx)
240+
self.add_index(model, idx, column_prefix=column_prefix, parent_model=parent_model)
237241

238242
def _add_field_index(self, model, field, *, column_prefix=""):
239243
"""Add an index on a field with db_index=True."""

tests/schema_/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class Address(models.Model):
1818
unique_together_one = models.CharField(max_length=10)
1919
unique_together_two = models.CharField(max_length=10)
2020
indexed_by_index_one = models.CharField(max_length=10)
21+
unique_constraint_one = models.CharField(max_length=10)
2122

2223
class Meta:
2324
apps = new_apps
25+
constraints = [models.UniqueConstraint(fields=["unique_constraint_one"], name="unique_one")]
2426
indexes = [models.Index(fields=["indexed_by_index_one"])]
2527
unique_together = [("unique_together_one", "unique_together_two")]
2628

@@ -33,9 +35,11 @@ class Author(models.Model):
3335
unique_together_three = models.CharField(max_length=10)
3436
unique_together_four = models.CharField(max_length=10)
3537
indexed_by_index_two = models.CharField(max_length=10)
38+
unique_constraint_two = models.CharField(max_length=10)
3639

3740
class Meta:
3841
apps = new_apps
42+
constraints = [models.UniqueConstraint(fields=["unique_constraint_two"], name="unique_two")]
3943
indexes = [models.Index(fields=["indexed_by_index_two"])]
4044
unique_together = [("unique_together_three", "unique_together_four")]
4145

tests/schema_/test_embedded_model.py

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import itertools
22

3-
from django.db import connection
4-
from django.test import TransactionTestCase
3+
from django.db import connection, models
4+
from django.test import TransactionTestCase, ignore_warnings
5+
from django.test.utils import isolate_apps
6+
from django.utils.deprecation import RemovedInDjango51Warning
7+
8+
from django_mongodb_backend.fields import EmbeddedModelField
59

610
from .models import Address, Author, Book, new_apps
711

@@ -199,6 +203,54 @@ def test_unique(self):
199203
editor.delete_model(Book)
200204
self.assertTableNotExists(Author)
201205

206+
@ignore_warnings(category=RemovedInDjango51Warning)
207+
@isolate_apps("schema_")
208+
def test_index_together(self):
209+
"""Meta.index_together on an embedded model."""
210+
211+
class Address(models.Model):
212+
index_together_one = models.CharField(max_length=10)
213+
index_together_two = models.CharField(max_length=10)
214+
215+
class Meta:
216+
app_label = "schema"
217+
index_together = [("index_together_one", "index_together_two")]
218+
219+
class Author(models.Model):
220+
address = EmbeddedModelField(Address)
221+
index_together_three = models.CharField(max_length=10)
222+
index_together_four = models.CharField(max_length=10)
223+
224+
class Meta:
225+
app_label = "schema"
226+
index_together = [("index_together_three", "index_together_four")]
227+
228+
class Book(models.Model):
229+
author = EmbeddedModelField(Author)
230+
231+
class Meta:
232+
app_label = "schema"
233+
234+
with connection.schema_editor() as editor:
235+
editor.create_model(Book)
236+
self.assertTableExists(Book)
237+
# Embedded uniques are created.
238+
self.assertEqual(
239+
self.get_constraints_for_columns(
240+
Book, ["author.address.index_together_one", "author.address.index_together_two"]
241+
),
242+
["schema_addr_index_t_a0305a_idx"],
243+
)
244+
self.assertEqual(
245+
self.get_constraints_for_columns(
246+
Book,
247+
["author.index_together_three", "author.index_together_four"],
248+
),
249+
["schema_auth_index_t_65817b_idx"],
250+
)
251+
editor.delete_model(Author)
252+
self.assertTableNotExists(Author)
253+
202254
def test_unique_together(self):
203255
"""Meta.unique_together on an embedded model."""
204256
with connection.schema_editor() as editor:
@@ -225,7 +277,7 @@ def test_unique_together(self):
225277
editor.delete_model(Author)
226278
self.assertTableNotExists(Author)
227279

228-
def test_index(self):
280+
def test_indexes(self):
229281
"""Meta.indexes on an embedded model."""
230282
with connection.schema_editor() as editor:
231283
editor.create_model(Book)
@@ -244,3 +296,23 @@ def test_index(self):
244296
)
245297
editor.delete_model(Author)
246298
self.assertTableNotExists(Author)
299+
300+
def test_constraints(self):
301+
"""Meta.constraints on an embedded model."""
302+
with connection.schema_editor() as editor:
303+
editor.create_model(Book)
304+
self.assertTableExists(Book)
305+
# Embedded uniques are created.
306+
self.assertEqual(
307+
self.get_constraints_for_columns(Book, ["author.unique_constraint_two"]),
308+
["unique_two"],
309+
)
310+
self.assertEqual(
311+
self.get_constraints_for_columns(
312+
Book,
313+
["author.address.unique_constraint_one"],
314+
),
315+
["unique_one"],
316+
)
317+
editor.delete_model(Author)
318+
self.assertTableNotExists(Author)

0 commit comments

Comments
 (0)