Fixed #32256 -- Fixed migration optimization crash when swapping field names.

This disables optimization of RenameField operation when an old field
name is referenced in subsequent operations.

Co-authored-by: InvalidInterrupt <InvalidInterrupt@users.noreply.github.com>
This commit is contained in:
Hasan Ramezani 2020-12-13 22:41:20 +01:00 committed by Mariusz Felisiak
parent ab58f07250
commit 7c18b22e2f
2 changed files with 22 additions and 2 deletions

View File

@ -392,8 +392,11 @@ class RenameField(FieldOperation):
), ),
] ]
# Skip `FieldOperation.reduce` as we want to run `references_field` # Skip `FieldOperation.reduce` as we want to run `references_field`
# against self.new_name. # against self.old_name and self.new_name.
return ( return (
super(FieldOperation, self).reduce(operation, app_label) or super(FieldOperation, self).reduce(operation, app_label) or
not operation.references_field(self.model_name, self.new_name, app_label) not (
operation.references_field(self.model_name, self.old_name, app_label) or
operation.references_field(self.model_name, self.new_name, app_label)
)
) )

View File

@ -572,6 +572,23 @@ class OptimizerTests(SimpleTestCase):
], ],
) )
def test_swapping_fields_names(self):
self.assertDoesNotOptimize(
[
migrations.CreateModel(
'MyModel',
[
('field_a', models.IntegerField()),
('field_b', models.IntegerField()),
],
),
migrations.RunPython(migrations.RunPython.noop),
migrations.RenameField('MyModel', 'field_a', 'field_c'),
migrations.RenameField('MyModel', 'field_b', 'field_a'),
migrations.RenameField('MyModel', 'field_c', 'field_b'),
],
)
def test_create_model_remove_field(self): def test_create_model_remove_field(self):
""" """
RemoveField should optimize into CreateModel. RemoveField should optimize into CreateModel.