mirror of
https://github.com/django/django.git
synced 2025-06-04 11:09:13 +00:00
Fixed #34946 -- Preserved db_default on combined default field addition.
Regression in 7414704e88d73dafbcfbb85f9bc54cb6111439d3.
This commit is contained in:
parent
656192c2c9
commit
8a28e983df
@ -744,9 +744,9 @@ class BaseDatabaseSchemaEditor:
|
|||||||
}
|
}
|
||||||
self.execute(sql, params)
|
self.execute(sql, params)
|
||||||
# Drop the default if we need to
|
# Drop the default if we need to
|
||||||
# (Django usually does not use in-database defaults)
|
|
||||||
if (
|
if (
|
||||||
not self.skip_default_on_alter(field)
|
field.db_default is NOT_PROVIDED
|
||||||
|
and not self.skip_default_on_alter(field)
|
||||||
and self.effective_default(field) is not None
|
and self.effective_default(field) is not None
|
||||||
):
|
):
|
||||||
changes_sql, params = self._alter_column_default_sql(
|
changes_sql, params = self._alter_column_default_sql(
|
||||||
|
@ -1692,15 +1692,22 @@ class OperationTests(OperationTestBase):
|
|||||||
field = new_state.models[app_label, "pony"].fields["height"]
|
field = new_state.models[app_label, "pony"].fields["height"]
|
||||||
self.assertEqual(field.default, 3)
|
self.assertEqual(field.default, 3)
|
||||||
self.assertEqual(field.db_default, Value(4))
|
self.assertEqual(field.db_default, Value(4))
|
||||||
project_state.apps.get_model(app_label, "pony").objects.create(weight=4)
|
pre_pony_pk = (
|
||||||
|
project_state.apps.get_model(app_label, "pony").objects.create(weight=4).pk
|
||||||
|
)
|
||||||
self.assertColumnNotExists(table_name, "height")
|
self.assertColumnNotExists(table_name, "height")
|
||||||
# Add field.
|
# Add field.
|
||||||
with connection.schema_editor() as editor:
|
with connection.schema_editor() as editor:
|
||||||
operation.database_forwards(app_label, editor, project_state, new_state)
|
operation.database_forwards(app_label, editor, project_state, new_state)
|
||||||
self.assertColumnExists(table_name, "height")
|
self.assertColumnExists(table_name, "height")
|
||||||
|
post_pony_pk = (
|
||||||
|
project_state.apps.get_model(app_label, "pony").objects.create(weight=10).pk
|
||||||
|
)
|
||||||
new_model = new_state.apps.get_model(app_label, "pony")
|
new_model = new_state.apps.get_model(app_label, "pony")
|
||||||
old_pony = new_model.objects.get()
|
pre_pony = new_model.objects.get(pk=pre_pony_pk)
|
||||||
self.assertEqual(old_pony.height, 4)
|
self.assertEqual(pre_pony.height, 4)
|
||||||
|
post_pony = new_model.objects.get(pk=post_pony_pk)
|
||||||
|
self.assertEqual(post_pony.height, 4)
|
||||||
new_pony = new_model.objects.create(weight=5)
|
new_pony = new_model.objects.create(weight=5)
|
||||||
if not connection.features.can_return_columns_from_insert:
|
if not connection.features.can_return_columns_from_insert:
|
||||||
new_pony.refresh_from_db()
|
new_pony.refresh_from_db()
|
||||||
|
@ -2257,6 +2257,23 @@ class SchemaTests(TransactionTestCase):
|
|||||||
columns = self.column_classes(AuthorDbDefault)
|
columns = self.column_classes(AuthorDbDefault)
|
||||||
self.assertEqual(columns["renamed_year"][1].default, "1985")
|
self.assertEqual(columns["renamed_year"][1].default, "1985")
|
||||||
|
|
||||||
|
@isolate_apps("schema")
|
||||||
|
def test_add_field_both_defaults_preserves_db_default(self):
|
||||||
|
class Author(Model):
|
||||||
|
class Meta:
|
||||||
|
app_label = "schema"
|
||||||
|
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
editor.create_model(Author)
|
||||||
|
|
||||||
|
field = IntegerField(default=1985, db_default=1988)
|
||||||
|
field.set_attributes_from_name("birth_year")
|
||||||
|
field.model = Author
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
editor.add_field(Author, field)
|
||||||
|
columns = self.column_classes(Author)
|
||||||
|
self.assertEqual(columns["birth_year"][1].default, "1988")
|
||||||
|
|
||||||
@skipUnlessDBFeature(
|
@skipUnlessDBFeature(
|
||||||
"supports_column_check_constraints", "can_introspect_check_constraints"
|
"supports_column_check_constraints", "can_introspect_check_constraints"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user