10
10
from .utils import OperationCollector
11
11
12
12
13
+ def ignore_embedded_models (func ):
14
+ """Make a SchemaEditor a no-op if model is an EmbeddedModel."""
15
+
16
+ def wrapper (self , model , * args , ** kwargs ):
17
+ # If parent_model isn't None, this is a valid recursive operation.
18
+ parent_model = kwargs .get ("parent_model" )
19
+ from .models import EmbeddedModel
20
+
21
+ if parent_model is None and issubclass (model , EmbeddedModel ):
22
+ return
23
+ func (self , model , * args , ** kwargs )
24
+
25
+ return wrapper
26
+
27
+
13
28
class DatabaseSchemaEditor (BaseDatabaseSchemaEditor ):
14
29
def get_collection (self , name ):
15
30
if self .collect_sql :
@@ -22,6 +37,7 @@ def get_database(self):
22
37
return self .connection .get_database ()
23
38
24
39
@wrap_database_errors
40
+ @ignore_embedded_models
25
41
def create_model (self , model ):
26
42
self .get_database ().create_collection (model ._meta .db_table )
27
43
self ._create_model_indexes (model )
@@ -75,13 +91,15 @@ def _create_model_indexes(self, model, column_prefix="", parent_model=None):
75
91
for index in model ._meta .indexes :
76
92
self .add_index (model , index , column_prefix = column_prefix , parent_model = parent_model )
77
93
94
+ @ignore_embedded_models
78
95
def delete_model (self , model ):
79
96
# Delete implicit M2m tables.
80
97
for field in model ._meta .local_many_to_many :
81
98
if field .remote_field .through ._meta .auto_created :
82
99
self .delete_model (field .remote_field .through )
83
100
self .get_collection (model ._meta .db_table ).drop ()
84
101
102
+ @ignore_embedded_models
85
103
def add_field (self , model , field ):
86
104
# Create implicit M2M tables.
87
105
if field .many_to_many and field .remote_field .through ._meta .auto_created :
@@ -103,6 +121,7 @@ def add_field(self, model, field):
103
121
elif self ._field_should_have_unique (field ):
104
122
self ._add_field_unique (model , field )
105
123
124
+ @ignore_embedded_models
106
125
def _alter_field (
107
126
self ,
108
127
model ,
@@ -149,6 +168,7 @@ def _alter_field(
149
168
if not old_field_unique and new_field_unique :
150
169
self ._add_field_unique (model , new_field )
151
170
171
+ @ignore_embedded_models
152
172
def remove_field (self , model , field ):
153
173
# Remove implicit M2M tables.
154
174
if field .many_to_many and field .remote_field .through ._meta .auto_created :
@@ -210,6 +230,7 @@ def _remove_model_indexes(self, model, column_prefix="", parent_model=None):
210
230
for index in model ._meta .indexes :
211
231
self .remove_index (parent_model or model , index )
212
232
233
+ @ignore_embedded_models
213
234
def alter_index_together (self , model , old_index_together , new_index_together , column_prefix = "" ):
214
235
olds = {tuple (fields ) for fields in old_index_together }
215
236
news = {tuple (fields ) for fields in new_index_together }
@@ -222,6 +243,7 @@ def alter_index_together(self, model, old_index_together, new_index_together, co
222
243
for field_names in news .difference (olds ):
223
244
self ._add_composed_index (model , field_names , column_prefix = column_prefix )
224
245
246
+ @ignore_embedded_models
225
247
def alter_unique_together (
226
248
self , model , old_unique_together , new_unique_together , column_prefix = "" , parent_model = None
227
249
):
@@ -249,6 +271,7 @@ def alter_unique_together(
249
271
model , constraint , parent_model = parent_model , column_prefix = column_prefix
250
272
)
251
273
274
+ @ignore_embedded_models
252
275
def add_index (
253
276
self , model , index , * , field = None , unique = False , column_prefix = "" , parent_model = None
254
277
):
@@ -302,6 +325,7 @@ def _add_field_index(self, model, field, *, column_prefix=""):
302
325
index .name = self ._create_index_name (model ._meta .db_table , [column_prefix + field .column ])
303
326
self .add_index (model , index , field = field , column_prefix = column_prefix )
304
327
328
+ @ignore_embedded_models
305
329
def remove_index (self , model , index ):
306
330
if index .contains_expressions :
307
331
return
@@ -355,6 +379,7 @@ def _remove_field_index(self, model, field, column_prefix=""):
355
379
)
356
380
collection .drop_index (index_names [0 ])
357
381
382
+ @ignore_embedded_models
358
383
def add_constraint (self , model , constraint , field = None , column_prefix = "" , parent_model = None ):
359
384
if isinstance (constraint , UniqueConstraint ) and self ._unique_supported (
360
385
condition = constraint .condition ,
@@ -384,6 +409,7 @@ def _add_field_unique(self, model, field, column_prefix=""):
384
409
constraint = UniqueConstraint (fields = [field .name ], name = name )
385
410
self .add_constraint (model , constraint , field = field , column_prefix = column_prefix )
386
411
412
+ @ignore_embedded_models
387
413
def remove_constraint (self , model , constraint ):
388
414
if isinstance (constraint , UniqueConstraint ) and self ._unique_supported (
389
415
condition = constraint .condition ,
@@ -417,6 +443,7 @@ def _remove_field_unique(self, model, field, column_prefix=""):
417
443
)
418
444
self .get_collection (model ._meta .db_table ).drop_index (constraint_names [0 ])
419
445
446
+ @ignore_embedded_models
420
447
def alter_db_table (self , model , old_db_table , new_db_table ):
421
448
if old_db_table == new_db_table :
422
449
return
0 commit comments