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

Method _alter_field

django/db/backends/base/schema.py:953–1322  ·  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

951 return None
952
953 def _alter_field(
954 self,
955 model,
956 old_field,
957 new_field,
958 old_type,
959 new_type,
960 old_db_params,
961 new_db_params,
962 strict=False,
963 ):
964 """Perform a "physical" (non-ManyToMany) field update."""
965 # Drop any FK constraints, we'll remake them later
966 fks_dropped = set()
967 if (
968 self.connection.features.supports_foreign_keys
969 and old_field.remote_field
970 and old_field.db_constraint
971 and self._field_should_be_altered(
972 old_field,
973 new_field,
974 ignore={"db_comment"},
975 )
976 ):
977 fk_names = self._constraint_names(
978 model, [old_field.column], foreign_key=True
979 )
980 if strict and len(fk_names) != 1:
981 raise ValueError(
982 "Found wrong number (%s) of foreign key constraints for %s.%s"
983 % (
984 len(fk_names),
985 model._meta.db_table,
986 old_field.column,
987 )
988 )
989 for fk_name in fk_names:
990 fks_dropped.add((old_field.column,))
991 self.execute(self._delete_fk_sql(model, fk_name))
992 # Has unique been removed?
993 if old_field.unique and (
994 not new_field.unique or self._field_became_primary_key(old_field, new_field)
995 ):
996 # Find the unique constraint for this field
997 meta_constraint_names = {
998 constraint.name for constraint in model._meta.constraints
999 }
1000 constraint_names = self._constraint_names(
1001 model,
1002 [old_field.column],
1003 unique=True,
1004 primary_key=False,
1005 exclude=meta_constraint_names,
1006 )
1007 if strict and len(constraint_names) != 1:
1008 raise ValueError(
1009 "Found wrong number (%s) of unique constraints for %s.%s"
1010 % (

Callers 1

alter_fieldMethod · 0.95

Calls 15

_constraint_namesMethod · 0.95
executeMethod · 0.95
_delete_fk_sqlMethod · 0.95
_delete_unique_sqlMethod · 0.95
_delete_index_sqlMethod · 0.95
_field_db_checkMethod · 0.95
_delete_check_sqlMethod · 0.95
_rename_field_sqlMethod · 0.95

Tested by

no test coverage detected