From 7c18b22e2fa70aa8dcfadb33beb17933abdf7ee4 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Sun, 13 Dec 2020 22:41:20 +0100 Subject: [PATCH] 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 --- django/db/migrations/operations/fields.py | 7 +++++-- tests/migrations/test_optimizer.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py index 8e494fd595..728105ea05 100644 --- a/django/db/migrations/operations/fields.py +++ b/django/db/migrations/operations/fields.py @@ -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) + ) ) diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index 9bb1aafebd..0caaf66f9e 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -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.