Fixed #27195 -- Stopped dropping default when adding a nullable column.

Thanks Rob Golding from Zapier for the report.
This commit is contained in:
Simon Charette 2016-09-08 00:57:04 -04:00
parent 331ca5391e
commit aca939b6e5
2 changed files with 7 additions and 2 deletions

View File

@ -420,7 +420,7 @@ class BaseDatabaseSchemaEditor(object):
self.execute(sql, params)
# Drop the default if we need to
# (Django usually does not use in-database defaults)
if not self.skip_default(field) and field.default is not None:
if not self.skip_default(field) and self.effective_default(field) is not None:
sql = self.sql_alter_column % {
"table": self.quote_name(model._meta.db_table),
"changes": self.sql_alter_column_no_default % {

View File

@ -21,6 +21,7 @@ from django.db.transaction import atomic
from django.test import (
TransactionTestCase, mock, skipIfDBFeature, skipUnlessDBFeature,
)
from django.test.utils import CaptureQueriesContext
from django.utils.timezone import UTC
from .fields import (
@ -347,8 +348,12 @@ class SchemaTests(TransactionTestCase):
# Add the new field
new_field = IntegerField(null=True)
new_field.set_attributes_from_name("age")
with connection.schema_editor() as editor:
with CaptureQueriesContext(connection) as ctx, connection.schema_editor() as editor:
editor.add_field(Author, new_field)
drop_default_sql = editor.sql_alter_column_no_default % {
'column': editor.quote_name(new_field.name),
}
self.assertFalse(any(drop_default_sql in query['sql'] for query in ctx.captured_queries))
# Ensure the field is right afterwards
columns = self.column_classes(Author)
self.assertEqual(columns['age'][0], "IntegerField")