diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 353b992258..4d1ac66f11 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -1131,6 +1131,7 @@ class MigrationAutodetector: # You can't just add NOT NULL fields with no default or fields # which don't allow empty strings as default. time_fields = (models.DateField, models.DateTimeField, models.TimeField) + auto_fields = (models.AutoField, models.SmallAutoField, models.BigAutoField) preserve_default = ( field.null or field.has_default() @@ -1138,6 +1139,7 @@ class MigrationAutodetector: or field.many_to_many or (field.blank and field.empty_strings_allowed) or (isinstance(field, time_fields) and field.auto_now) + or (isinstance(field, auto_fields)) ) if not preserve_default: field = field.clone() diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index d4345208ca..aa0748a19f 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -1595,6 +1595,28 @@ class AutodetectorTests(BaseAutodetectorTests): changes, "testapp", 0, 0, db_default="Ada Lovelace" ) + @mock.patch( + "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition", + side_effect=AssertionError("Should not have prompted for not null addition"), + ) + def test_remove_primary_key_attribute(self, mocked_ask_method): + initial_state = ModelState( + "testapp", + "Author", + [ + ("pkfield", models.IntegerField(primary_key=True)), + ], + ) + updated_state = ModelState( + "testapp", + "Author", + [ + ("id", models.AutoField(primary_key=True)), + ("pkfield", models.IntegerField(primary_key=False)), + ], + ) + changes = self.get_changes([initial_state], [updated_state]) + @mock.patch( "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", return_value=models.NOT_PROVIDED,