From 2156565b5bac2e6f503ad4841e2b6ed44e4de656 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 28 Mar 2018 00:58:12 -0400 Subject: [PATCH] Fixed #29245 -- Made autodetector treat field renames + db_column addition as RenameField. --- django/db/migrations/autodetector.py | 11 ++++- tests/migrations/test_autodetector.py | 61 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 6cd92206ec..f32d4af9eb 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -797,12 +797,19 @@ class MigrationAutodetector: field_dec = self.deep_deconstruct(field) for rem_app_label, rem_model_name, rem_field_name in sorted(self.old_field_keys - self.new_field_keys): if rem_app_label == app_label and rem_model_name == model_name: - old_field_dec = self.deep_deconstruct(old_model_state.get_field_by_name(rem_field_name)) + old_field = old_model_state.get_field_by_name(rem_field_name) + old_field_dec = self.deep_deconstruct(old_field) if field.remote_field and field.remote_field.model and 'to' in old_field_dec[2]: old_rel_to = old_field_dec[2]['to'] if old_rel_to in self.renamed_models_rel: old_field_dec[2]['to'] = self.renamed_models_rel[old_rel_to] - if old_field_dec == field_dec: + old_field.set_attributes_from_name(rem_field_name) + old_db_column = old_field.get_attname_column()[1] + if (old_field_dec == field_dec or ( + # Was the field renamed and db_column equal to the + # old field's column added? + old_field_dec[0:2] == field_dec[0:2] and + dict(old_field_dec[2], db_column=old_db_column) == field_dec[2])): if self.questioner.ask_rename(model_name, rem_field_name, field_name, field): self.add_operation( app_label, diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index e262a8ee52..fd1bc383b9 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -921,6 +921,67 @@ class AutodetectorTests(TestCase): changes, 'app', 0, 1, model_name='bar', old_name='second', new_name='second_renamed', ) + def test_rename_field_preserved_db_column(self): + """ + RenameField is used if a field is renamed and db_column equal to the + old field's column is added. + """ + before = [ + ModelState('app', 'Foo', [ + ('id', models.AutoField(primary_key=True)), + ('field', models.IntegerField()), + ]), + ] + after = [ + ModelState('app', 'Foo', [ + ('id', models.AutoField(primary_key=True)), + ('renamed_field', models.IntegerField(db_column='field')), + ]), + ] + changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True})) + self.assertNumberMigrations(changes, 'app', 1) + self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField']) + self.assertOperationAttributes( + changes, 'app', 0, 0, model_name='foo', old_name='field', new_name='renamed_field', + ) + self.assertOperationAttributes(changes, 'app', 0, 1, model_name='foo', name='renamed_field') + self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), ( + 'renamed_field', 'django.db.models.IntegerField', [], {'db_column': 'field'}, + )) + + def test_rename_related_field_preserved_db_column(self): + before = [ + ModelState('app', 'Foo', [ + ('id', models.AutoField(primary_key=True)), + ]), + ModelState('app', 'Bar', [ + ('id', models.AutoField(primary_key=True)), + ('foo', models.ForeignKey('app.Foo', models.CASCADE)), + ]), + ] + after = [ + ModelState('app', 'Foo', [ + ('id', models.AutoField(primary_key=True)), + ]), + ModelState('app', 'Bar', [ + ('id', models.AutoField(primary_key=True)), + ('renamed_foo', models.ForeignKey('app.Foo', models.CASCADE, db_column='foo_id')), + ]), + ] + changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True})) + self.assertNumberMigrations(changes, 'app', 1) + self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField']) + self.assertOperationAttributes( + changes, 'app', 0, 0, model_name='bar', old_name='foo', new_name='renamed_foo', + ) + self.assertOperationAttributes(changes, 'app', 0, 1, model_name='bar', name='renamed_foo') + self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), ( + 'renamed_foo', + 'django.db.models.ForeignKey', + [], + {'to': 'app.Foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'}, + )) + def test_rename_model(self): """Tests autodetection of renamed models.""" changes = self.get_changes(