10
10
from .utils import OperationCollector
11
11
12
12
13
+ def ignore_embedded_models (func ):
14
+ """
15
+ Make a SchemaEditor method a no-op if model is an EmbeddedModel (unless
16
+ parent_model isn't None, in which case this is a valid recursive operation
17
+ such as adding an index on an embedded model's field).
18
+ """
19
+
20
+ def wrapper (self , model , * args , ** kwargs ):
21
+ parent_model = kwargs .get ("parent_model" )
22
+ from .models import EmbeddedModel
23
+
24
+ if issubclass (model , EmbeddedModel ) and parent_model is None :
25
+ return
26
+ func (self , model , * args , ** kwargs )
27
+
28
+ return wrapper
29
+
30
+
13
31
class DatabaseSchemaEditor (BaseDatabaseSchemaEditor ):
14
32
def get_collection (self , name ):
15
33
if self .collect_sql :
@@ -22,6 +40,7 @@ def get_database(self):
22
40
return self .connection .get_database ()
23
41
24
42
@wrap_database_errors
43
+ @ignore_embedded_models
25
44
def create_model (self , model ):
26
45
self .get_database ().create_collection (model ._meta .db_table )
27
46
self ._create_model_indexes (model )
@@ -75,13 +94,15 @@ def _create_model_indexes(self, model, column_prefix="", parent_model=None):
75
94
for index in model ._meta .indexes :
76
95
self .add_index (model , index , column_prefix = column_prefix , parent_model = parent_model )
77
96
97
+ @ignore_embedded_models
78
98
def delete_model (self , model ):
79
99
# Delete implicit M2m tables.
80
100
for field in model ._meta .local_many_to_many :
81
101
if field .remote_field .through ._meta .auto_created :
82
102
self .delete_model (field .remote_field .through )
83
103
self .get_collection (model ._meta .db_table ).drop ()
84
104
105
+ @ignore_embedded_models
85
106
def add_field (self , model , field ):
86
107
# Create implicit M2M tables.
87
108
if field .many_to_many and field .remote_field .through ._meta .auto_created :
@@ -103,6 +124,7 @@ def add_field(self, model, field):
103
124
elif self ._field_should_have_unique (field ):
104
125
self ._add_field_unique (model , field )
105
126
127
+ @ignore_embedded_models
106
128
def _alter_field (
107
129
self ,
108
130
model ,
@@ -149,6 +171,7 @@ def _alter_field(
149
171
if not old_field_unique and new_field_unique :
150
172
self ._add_field_unique (model , new_field )
151
173
174
+ @ignore_embedded_models
152
175
def remove_field (self , model , field ):
153
176
# Remove implicit M2M tables.
154
177
if field .many_to_many and field .remote_field .through ._meta .auto_created :
@@ -210,6 +233,7 @@ def _remove_model_indexes(self, model, column_prefix="", parent_model=None):
210
233
for index in model ._meta .indexes :
211
234
self .remove_index (parent_model or model , index )
212
235
236
+ @ignore_embedded_models
213
237
def alter_index_together (self , model , old_index_together , new_index_together , column_prefix = "" ):
214
238
olds = {tuple (fields ) for fields in old_index_together }
215
239
news = {tuple (fields ) for fields in new_index_together }
@@ -222,6 +246,7 @@ def alter_index_together(self, model, old_index_together, new_index_together, co
222
246
for field_names in news .difference (olds ):
223
247
self ._add_composed_index (model , field_names , column_prefix = column_prefix )
224
248
249
+ @ignore_embedded_models
225
250
def alter_unique_together (
226
251
self , model , old_unique_together , new_unique_together , column_prefix = "" , parent_model = None
227
252
):
@@ -249,6 +274,7 @@ def alter_unique_together(
249
274
model , constraint , parent_model = parent_model , column_prefix = column_prefix
250
275
)
251
276
277
+ @ignore_embedded_models
252
278
def add_index (
253
279
self , model , index , * , field = None , unique = False , column_prefix = "" , parent_model = None
254
280
):
@@ -302,6 +328,7 @@ def _add_field_index(self, model, field, *, column_prefix=""):
302
328
index .name = self ._create_index_name (model ._meta .db_table , [column_prefix + field .column ])
303
329
self .add_index (model , index , field = field , column_prefix = column_prefix )
304
330
331
+ @ignore_embedded_models
305
332
def remove_index (self , model , index ):
306
333
if index .contains_expressions :
307
334
return
@@ -355,6 +382,7 @@ def _remove_field_index(self, model, field, column_prefix=""):
355
382
)
356
383
collection .drop_index (index_names [0 ])
357
384
385
+ @ignore_embedded_models
358
386
def add_constraint (self , model , constraint , field = None , column_prefix = "" , parent_model = None ):
359
387
if isinstance (constraint , UniqueConstraint ) and self ._unique_supported (
360
388
condition = constraint .condition ,
@@ -384,6 +412,7 @@ def _add_field_unique(self, model, field, column_prefix=""):
384
412
constraint = UniqueConstraint (fields = [field .name ], name = name )
385
413
self .add_constraint (model , constraint , field = field , column_prefix = column_prefix )
386
414
415
+ @ignore_embedded_models
387
416
def remove_constraint (self , model , constraint ):
388
417
if isinstance (constraint , UniqueConstraint ) and self ._unique_supported (
389
418
condition = constraint .condition ,
@@ -417,6 +446,7 @@ def _remove_field_unique(self, model, field, column_prefix=""):
417
446
)
418
447
self .get_collection (model ._meta .db_table ).drop_index (constraint_names [0 ])
419
448
449
+ @ignore_embedded_models
420
450
def alter_db_table (self , model , old_db_table , new_db_table ):
421
451
if old_db_table == new_db_table :
422
452
return
0 commit comments