mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +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:
committed by
Sarah Boyce
parent
9aeb38c296
commit
91a4b9a8ec
@@ -5890,6 +5890,50 @@ class OperationTests(OperationTestBase):
|
||||
def test_invalid_generated_field_changes_virtual(self):
|
||||
self._test_invalid_generated_field_changes(db_persist=False)
|
||||
|
||||
def _test_invalid_generated_field_changes_on_rename(self, db_persist):
|
||||
app_label = "test_igfcor"
|
||||
operation = migrations.AddField(
|
||||
"Pony",
|
||||
"modified_pink",
|
||||
models.GeneratedField(
|
||||
expression=F("pink") + F("pink"),
|
||||
output_field=models.IntegerField(),
|
||||
db_persist=db_persist,
|
||||
),
|
||||
)
|
||||
project_state, new_state = self.make_test_state(app_label, operation)
|
||||
# Add generated column.
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards(app_label, editor, project_state, new_state)
|
||||
# Rename field used in the generated field.
|
||||
operations = [
|
||||
migrations.RenameField("Pony", "pink", "renamed_pink"),
|
||||
migrations.AlterField(
|
||||
"Pony",
|
||||
"modified_pink",
|
||||
models.GeneratedField(
|
||||
expression=F("renamed_pink"),
|
||||
output_field=models.IntegerField(),
|
||||
db_persist=db_persist,
|
||||
),
|
||||
),
|
||||
]
|
||||
msg = (
|
||||
"Modifying GeneratedFields is not supported - the field "
|
||||
f"{app_label}.Pony.modified_pink must be removed and re-added with the "
|
||||
"new definition."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
self.apply_operations(app_label, new_state, operations)
|
||||
|
||||
@skipUnlessDBFeature("supports_stored_generated_columns")
|
||||
def test_invalid_generated_field_changes_on_rename_stored(self):
|
||||
self._test_invalid_generated_field_changes_on_rename(db_persist=True)
|
||||
|
||||
@skipUnlessDBFeature("supports_virtual_generated_columns")
|
||||
def test_invalid_generated_field_changes_on_rename_virtual(self):
|
||||
self._test_invalid_generated_field_changes_on_rename(db_persist=False)
|
||||
|
||||
@skipUnlessDBFeature(
|
||||
"supports_stored_generated_columns",
|
||||
"supports_virtual_generated_columns",
|
||||
|
||||
Reference in New Issue
Block a user