1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #470 -- Added support for database defaults on fields.

Special thanks to Hannes Ljungberg for finding multiple implementation
gaps.

Thanks also to Simon Charette, Adam Johnson, and Mariusz Felisiak for
reviews.
This commit is contained in:
Ian Foote
2020-11-22 22:27:57 +00:00
committed by Mariusz Felisiak
parent 599f3e2cda
commit 7414704e88
32 changed files with 1089 additions and 34 deletions

View File

@@ -269,6 +269,14 @@ class AutodetectorTests(BaseAutodetectorTests):
("name", models.CharField(max_length=200, default="Ada Lovelace")),
],
)
author_name_db_default = ModelState(
"testapp",
"Author",
[
("id", models.AutoField(primary_key=True)),
("name", models.CharField(max_length=200, db_default="Ada Lovelace")),
],
)
author_name_check_constraint = ModelState(
"testapp",
"Author",
@@ -1289,6 +1297,21 @@ class AutodetectorTests(BaseAutodetectorTests):
self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
@mock.patch(
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
side_effect=AssertionError("Should not have prompted for not null addition"),
)
def test_add_not_null_field_with_db_default(self, mocked_ask_method):
changes = self.get_changes([self.author_empty], [self.author_name_db_default])
self.assertNumberMigrations(changes, "testapp", 1)
self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
self.assertOperationAttributes(
changes, "testapp", 0, 0, name="name", preserve_default=True
)
self.assertOperationFieldAttributes(
changes, "testapp", 0, 0, db_default=models.Value("Ada Lovelace")
)
@mock.patch(
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
side_effect=AssertionError("Should not have prompted for not null addition"),
@@ -1478,6 +1501,23 @@ class AutodetectorTests(BaseAutodetectorTests):
changes, "testapp", 0, 0, default="Ada Lovelace"
)
@mock.patch(
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
side_effect=AssertionError("Should not have prompted for not null alteration"),
)
def test_alter_field_to_not_null_with_db_default(self, mocked_ask_method):
changes = self.get_changes(
[self.author_name_null], [self.author_name_db_default]
)
self.assertNumberMigrations(changes, "testapp", 1)
self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
self.assertOperationAttributes(
changes, "testapp", 0, 0, name="name", preserve_default=True
)
self.assertOperationFieldAttributes(
changes, "testapp", 0, 0, db_default=models.Value("Ada Lovelace")
)
@mock.patch(
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
return_value=models.NOT_PROVIDED,