mirror of https://github.com/django/django.git
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:
parent
ab58f07250
commit
7c18b22e2f
|
@ -392,8 +392,11 @@ class RenameField(FieldOperation):
|
|||
),
|
||||
]
|
||||
# Skip `FieldOperation.reduce` as we want to run `references_field`
|
||||
# against self.new_name.
|
||||
# against self.old_name and self.new_name.
|
||||
return (
|
||||
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)
|
||||
)
|
||||
)
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
RemoveField should optimize into CreateModel.
|
||||
|
|
Loading…
Reference in New Issue