summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Safronov <hoskeowl@gmail.com>2022-02-01 00:29:49 +0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-01 07:33:22 +0100
commit6928227dffe45f06deed9a218a188e6ed11334ba (patch)
treed7ec103d4d18277d69345f4208c89f8f5b9b998f
parentaff79be03a8c787ba0cc7e00dcec9eeb8ad01c87 (diff)
downloaddjango-6928227dffe45f06deed9a218a188e6ed11334ba.tar.gz
[4.0.x] Fixed #33480 -- Fixed makemigrations crash when renaming field of renamed model.
Regression in aa4acc164d1247c0de515c959f7b09648b57dc42. Backport of 97a72744681d0993b50dee952cf32cdf9650ad9f from main
-rw-r--r--django/db/migrations/autodetector.py2
-rw-r--r--docs/releases/4.0.2.txt3
-rw-r--r--tests/migrations/test_autodetector.py20
3 files changed, 24 insertions, 1 deletions
diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
index 96cb463848..408f63185a 100644
--- a/django/db/migrations/autodetector.py
+++ b/django/db/migrations/autodetector.py
@@ -824,7 +824,7 @@ class MigrationAutodetector:
for app_label, model_name, field_name in sorted(self.new_field_keys - self.old_field_keys):
old_model_name = self.renamed_models.get((app_label, model_name), model_name)
old_model_state = self.from_state.models[app_label, old_model_name]
- new_model_state = self.to_state.models[app_label, old_model_name]
+ new_model_state = self.to_state.models[app_label, model_name]
field = new_model_state.get_field(field_name)
# Scan to see if this is actually a rename!
field_dec = self.deep_deconstruct(field)
diff --git a/docs/releases/4.0.2.txt b/docs/releases/4.0.2.txt
index e7b9879625..c5176e01fc 100644
--- a/docs/releases/4.0.2.txt
+++ b/docs/releases/4.0.2.txt
@@ -37,3 +37,6 @@ Bugfixes
* Fixed a bug in Django 4.0 that caused a crash of ``QuerySet.aggregate()``
after ``annotate()`` on an aggregate function with a
:ref:`default <aggregate-default>` (:ticket:`33468`).
+
+* Fixed a regression in Django 4.0 that caused a crash of ``makemigrations``
+ when renaming a field of a renamed model (:ticket:`33480`).
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index e0cf9d49ef..766d21fb3b 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -1043,6 +1043,26 @@ class AutodetectorTests(TestCase):
{'to': 'app.foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'},
))
+ def test_rename_field_with_renamed_model(self):
+ changes = self.get_changes(
+ [self.author_name],
+ [
+ ModelState('testapp', 'RenamedAuthor', [
+ ('id', models.AutoField(primary_key=True)),
+ ('renamed_name', models.CharField(max_length=200)),
+ ]),
+ ],
+ MigrationQuestioner({'ask_rename_model': True, 'ask_rename': True}),
+ )
+ self.assertNumberMigrations(changes, 'testapp', 1)
+ self.assertOperationTypes(changes, 'testapp', 0, ['RenameModel', 'RenameField'])
+ self.assertOperationAttributes(
+ changes, 'testapp', 0, 0, old_name='Author', new_name='RenamedAuthor',
+ )
+ self.assertOperationAttributes(
+ changes, 'testapp', 0, 1, old_name='name', new_name='renamed_name',
+ )
+
def test_rename_model(self):
"""Tests autodetection of renamed models."""
changes = self.get_changes(