8000 Refs #34320 -- Stopped recreating check constraints when renaming fie… · django/django@9953c80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9953c80

Browse files
authored
Refs #34320 -- Stopped recreating check constraints when renaming fields.
This also fixes test_rename_field_with_check_to_truncated_name() on MariaDB 10.5.2+ as ALTER TABLE ... RENAME COLUMN statement doesn't rename inline constraints.
1 parent ef00d6e commit 9953c80

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

django/db/backends/base/schema.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,17 @@ def alter_field(self, model, old_field, new_field, strict=False):
838838
strict,
839839
)
840840

841+
def _field_db_check(self, field, field_db_params):
842+
# Always check constraints with the same mocked column name to avoid
843+
# recreating constrains when the column is renamed.
844+
check_constraints = self.connection.data_type_check_constraints
845+
data = field.db_type_parameters(self.connection)
846+
data["column"] = "__column_name__"
847+
try:
848+
return check_constraints[field.get_internal_type()] % data
849+
except KeyError:
850+
return None
851+
841852
def _alter_field(
842853
self,
843854
model,
@@ -956,7 +967,9 @@ def _alter_field(
956967
# is to look at its name (refs #28053).
957968
self.execute(self._delete_index_sql(model, index_name))
958969
# Change check constraints?
959-
if old_db_params["check"] != new_db_params["check"] and old_db_params["check"]:
970+
old_db_check = self._field_db_check(old_field, old_db_params)
971+
new_db_check = self._field_db_check(new_field, new_db_params)
972+
if old_db_check != new_db_check and old_db_check:
960973
meta_constraint_names = {
961974
constraint.name for constraint in model._meta.constraints
962975
}
@@ -1162,7 +1175,7 @@ def _alter_field(
11621175
self._create_fk_sql(rel.related_model, rel.field, "_fk")
11631176
)
11641177
# Does it have check constraints we need to add?
1165-
if old_db_params["check"] != new_db_params["check"] and new_db_params["check"]:
1178+
if old_db_check != new_db_check and new_db_check:
11661179
constraint_name = self._create_index_name(
11671180
model._meta.db_table, [new_field.column], suffix="_check"
11681181
)

django/db/backends/mysql/schema.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ def _alter_column_type_sql(
230230
model, old_field, new_field, new_type, old_collation, new_collation
231231
)
232232

233+
def _field_db_check(self, field, field_db_params):
234+
if self.connection.mysql_is_mariadb and self.connection.mysql_version >= (
235+
10,
236+
5,
237+
2,
238+
):
239+
return super()._field_db_check(field, field_db_params)
240+
# On MySQL and MariaDB < 10.5.2 (no support for
241+
# "ALTER TABLE ... RENAME COLUMN" statements), check constraints with
242+
# the column name as it requires explicit recreation when the column is
243+
# renamed.
244+
return field_db_params["check"]
245+
233246
def _rename_field_sql(self, table, old_field, new_field, new_type):
234247
new_type = self._set_field_new_type_null_status(old_field, new_type)
235248
return super()._rename_field_sql(table, old_field, new_field, new_type)

0 commit comments

Comments
 (0)
0