MCPcopy
hub / github.com/django/django / add_field

Method add_field

django/db/backends/sqlite3/schema.py:302–331  ·  view source on GitHub ↗

Create a field on a model.

(self, model, field)

Source from the content-addressed store, hash-verified

300 self.deferred_sql.remove(sql)
301
302 def add_field(self, model, field):
303 """Create a field on a model."""
304 from django.db.models.expressions import Value
305
306 # Special-case implicit M2M tables.
307 if field.many_to_many and field.remote_field.through._meta.auto_created:
308 self.create_model(field.remote_field.through)
309 elif isinstance(field, CompositePrimaryKey):
310 # If a CompositePrimaryKey field was added, the existing primary
311 # key field had to be altered too, resulting in an AddField,
312 # AlterField migration. The table cannot be re-created on AddField,
313 # it would result in a duplicate primary key error.
314 return
315 elif (
316 # Primary keys and unique fields are not supported in ALTER TABLE
317 # ADD COLUMN.
318 field.primary_key
319 or field.unique
320 or not field.null
321 # Fields with default values cannot by handled by ALTER TABLE ADD
322 # COLUMN statement because DROP DEFAULT is not supported in
323 # ALTER TABLE.
324 or self.effective_default(field) is not None
325 # Fields with non-constant defaults cannot by handled by ALTER
326 # TABLE ADD COLUMN statement.
327 or (field.has_db_default() and not isinstance(field.db_default, Value))
328 ):
329 self._remake_table(model, create_field=field)
330 else:
331 super().add_field(model, field)
332
333 def remove_field(self, model, field):
334 """

Callers

nothing calls this directly

Calls 4

_remake_tableMethod · 0.95
effective_defaultMethod · 0.80
has_db_defaultMethod · 0.80
create_modelMethod · 0.45

Tested by

no test coverage detected