MCPcopy
hub / github.com/django/django / _alter_field

Method _alter_field

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

Perform a "physical" (non-ManyToMany) field update.

(
        self,
        model,
        old_field,
        new_field,
        old_type,
        new_type,
        old_db_params,
        new_db_params,
        strict=False,
    )

Source from the content-addressed store, hash-verified

358 self._remake_table(model, delete_field=field)
359
360 def _alter_field(
361 self,
362 model,
363 old_field,
364 new_field,
365 old_type,
366 new_type,
367 old_db_params,
368 new_db_params,
369 strict=False,
370 ):
371 """Perform a "physical" (non-ManyToMany) field update."""
372 # Use "ALTER TABLE ... RENAME COLUMN" if only the column name
373 # changed and there aren't any constraints.
374 if (
375 old_field.column != new_field.column
376 and self.column_sql(model, old_field) == self.column_sql(model, new_field)
377 and not (
378 old_field.remote_field
379 and old_field.db_constraint
380 or new_field.remote_field
381 and new_field.db_constraint
382 )
383 ):
384 return self.execute(
385 self._rename_field_sql(
386 model._meta.db_table, old_field, new_field, new_type
387 )
388 )
389 # Alter by remaking table
390 self._remake_table(model, alter_fields=[(old_field, new_field)])
391 # Rebuild tables with FKs pointing to this field.
392 old_collation = old_db_params.get("collation")
393 new_collation = new_db_params.get("collation")
394 if new_field.unique and (
395 old_type != new_type or old_collation != new_collation
396 ):
397 related_models = {
398 rel.related_model
399 for _, rel in _related_non_m2m_objects(old_field, new_field)
400 # Ignore self-relationship since the table was already rebuilt.
401 if rel.related_model != model
402 }
403 opts = new_field.model._meta
404 if new_field.primary_key:
405 for many_to_many in opts.many_to_many:
406 # Ignore self-relationship since the table was already
407 # rebuilt.
408 if many_to_many.related_model == model:
409 continue
410 if many_to_many.remote_field.through._meta.auto_created:
411 related_models.add(many_to_many.remote_field.through)
412 for related_model in related_models:
413 self._remake_table(related_model)
414
415 def _alter_many_to_many(self, model, old_field, new_field, strict):
416 """Alter M2Ms to repoint their to= endpoints."""

Callers

nothing calls this directly

Calls 7

executeMethod · 0.95
_rename_field_sqlMethod · 0.95
_remake_tableMethod · 0.95
_related_non_m2m_objectsFunction · 0.90
column_sqlMethod · 0.45
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected