mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	Refs #22997 -- Prevented requesting a default value for auto fields.
This commit is contained in:
		| @@ -1132,6 +1132,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() | ||||
| @@ -1139,6 +1140,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() | ||||
|   | ||||
| @@ -1595,6 +1595,34 @@ class AutodetectorTests(BaseAutodetectorTests): | ||||
|             changes, "testapp", 0, 0, db_default="Ada Lovelace" | ||||
|         ) | ||||
|  | ||||
|     @mock.patch( | ||||
|         "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition" | ||||
|     ) | ||||
|     def test_add_auto_field_does_not_request_default(self, mocked_ask_method): | ||||
|         initial_state = ModelState( | ||||
|             "testapp", | ||||
|             "Author", | ||||
|             [ | ||||
|                 ("pkfield", models.IntegerField(primary_key=True)), | ||||
|             ], | ||||
|         ) | ||||
|         for auto_field in [ | ||||
|             models.AutoField, | ||||
|             models.BigAutoField, | ||||
|             models.SmallAutoField, | ||||
|         ]: | ||||
|             with self.subTest(auto_field=auto_field): | ||||
|                 updated_state = ModelState( | ||||
|                     "testapp", | ||||
|                     "Author", | ||||
|                     [ | ||||
|                         ("id", auto_field(primary_key=True)), | ||||
|                         ("pkfield", models.IntegerField(primary_key=False)), | ||||
|                     ], | ||||
|                 ) | ||||
|                 self.get_changes([initial_state], [updated_state]) | ||||
|                 mocked_ask_method.assert_not_called() | ||||
|  | ||||
|     @mock.patch( | ||||
|         "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration", | ||||
|         return_value=models.NOT_PROVIDED, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user