diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 353b992258..a5646dc377 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -239,14 +239,14 @@ class MigrationAutodetector: self.through_users = {} self.old_field_keys = { (app_label, model_name, field_name) - for app_label, model_name in self.kept_model_keys + for app_label, model_name in self.kept_model_keys | self.kept_unmanaged_keys for field_name in self.from_state.models[ app_label, self.renamed_models.get((app_label, model_name), model_name) ].fields } self.new_field_keys = { (app_label, model_name, field_name) - for app_label, model_name in self.kept_model_keys + for app_label, model_name in self.kept_model_keys | self.kept_unmanaged_keys for field_name in self.to_state.models[app_label, model_name].fields } diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index d4345208ca..c60fa3e6b9 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -712,6 +712,26 @@ class AutodetectorTests(BaseAutodetectorTests): author_unmanaged = ModelState( "testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author",) ) + author_unmanaged_name_default = ModelState( + "testapp", + "Author", + [ + ("id", models.AutoField(primary_key=True)), + ("name", models.CharField(max_length=200, default="Ada Lovelace")), + ], + {"managed": False}, + ("testapp.author",), + ) + author_unmanaged_name_longer = ModelState( + "testapp", + "Author", + [ + ("id", models.AutoField(primary_key=True)), + ("name", models.CharField(max_length=400)), + ], + {"managed": False}, + ("testapp.author",), + ) author_unmanaged_managed = ModelState( "testapp", "AuthorUnmanaged", [], {}, ("testapp.author",) ) @@ -3520,7 +3540,7 @@ class AutodetectorTests(BaseAutodetectorTests): self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy") def test_unmanaged_create(self): - """The autodetector correctly deals with managed models.""" + """The autodetector correctly deals with unmanaged models.""" # First, we test adding an unmanaged model changes = self.get_changes( [self.author_empty], [self.author_empty, self.author_unmanaged] @@ -3532,6 +3552,18 @@ class AutodetectorTests(BaseAutodetectorTests): changes, "testapp", 0, 0, name="AuthorUnmanaged", options={"managed": False} ) + def test_unmanaged_alter_field(self): + """Tests autodetection of new fields on an unmanaged model.""" + changes = self.get_changes( + [self.author_unmanaged_name_default], [self.author_unmanaged_name_longer] + ) + # Right number/type of migrations? + self.assertNumberMigrations(changes, "testapp", 1) + self.assertOperationTypes(changes, "testapp", 0, ["AlterField"]) + self.assertOperationAttributes( + changes, "testapp", 0, 0, name="name", preserve_default=True + ) + def test_unmanaged_delete(self): changes = self.get_changes( [self.author_empty, self.author_unmanaged], [self.author_empty]