1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #35422 -- Fixed migrations crash when altering GeneratedField referencing rename field.

Thanks Sarah Boyce for the report and Simon Charette for the
implementation idea.
This commit is contained in:
Mariusz Felisiak
2024-05-02 20:31:51 +02:00
committed by Sarah Boyce
parent 9aeb38c296
commit 91a4b9a8ec
4 changed files with 73 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ import operator
from datetime import datetime
from django.conf import settings
from django.core.exceptions import FieldError
from django.db.backends.ddl_references import (
Columns,
Expressions,
@@ -831,6 +832,7 @@ class BaseDatabaseSchemaEditor:
old_type = old_db_params["type"]
new_db_params = new_field.db_parameters(connection=self.connection)
new_type = new_db_params["type"]
modifying_generated_field = False
if (old_type is None and old_field.remote_field is None) or (
new_type is None and new_field.remote_field is None
):
@@ -869,13 +871,19 @@ class BaseDatabaseSchemaEditor:
"through= on M2M fields)" % (old_field, new_field)
)
elif old_field.generated != new_field.generated or (
new_field.generated
and (
old_field.db_persist != new_field.db_persist
or old_field.generated_sql(self.connection)
!= new_field.generated_sql(self.connection)
)
new_field.generated and old_field.db_persist != new_field.db_persist
):
modifying_generated_field = True
elif new_field.generated:
try:
old_field_sql = old_field.generated_sql(self.connection)
except FieldError:
# Field used in a generated field was renamed.
modifying_generated_field = True
else:
new_field_sql = new_field.generated_sql(self.connection)
modifying_generated_field = old_field_sql != new_field_sql
if modifying_generated_field:
raise ValueError(
f"Modifying GeneratedFields is not supported - the field {new_field} "
"must be removed and re-added with the new definition."

View File

@@ -158,6 +158,18 @@ class DatabaseFeatures(BaseDatabaseFeatures):
},
}
)
if not self.connection.mysql_is_mariadb:
skips.update(
{
"MySQL doesn't allow renaming columns referenced by generated "
"columns": {
"migrations.test_operations.OperationTests."
"test_invalid_generated_field_changes_on_rename_stored",
"migrations.test_operations.OperationTests."
"test_invalid_generated_field_changes_on_rename_virtual",
},
}
)
return skips
@cached_property