Fixed #21783: Use defaults for adding NOT NULL on sqlite

This commit is contained in:
Andrew Godwin 2014-01-19 17:10:24 +00:00
parent c9de1b4a55
commit e802c97581
3 changed files with 33 additions and 2 deletions

View File

@ -226,7 +226,7 @@ class DatabaseOperations(BaseDatabaseOperations):
if isinstance(value, six.integer_types):
return str(value)
elif isinstance(value, six.string_types):
return six.text_type(value)
return '"%s"' % six.text_type(value)
elif isinstance(value, type(True)):
return str(int(value))
elif value is None:

View File

@ -29,6 +29,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# Add in any created fields
for field in create_fields:
body[field.name] = field
# If there's a default, insert it into the copy map
if field.get_default():
mapping[field.column] = self.connection.ops.quote_parameter(
field.get_default()
)
# Add in any altered fields
for (old_field, new_field) in alter_fields:
del body[old_field.name]

View File

@ -145,7 +145,7 @@ class SchemaTests(TransactionTestCase):
# Ensure there's no age field
columns = self.column_classes(Author)
self.assertNotIn("age", columns)
# Alter the name field to a TextField
# Add the new field
new_field = IntegerField(null=True)
new_field.set_attributes_from_name("age")
with connection.schema_editor() as editor:
@ -158,6 +158,32 @@ class SchemaTests(TransactionTestCase):
self.assertEqual(columns['age'][0], "IntegerField")
self.assertEqual(columns['age'][1][6], True)
def test_add_field_temp_default(self):
"""
Tests adding fields to models with a temporary default
"""
# Create the table
with connection.schema_editor() as editor:
editor.create_model(Author)
# Ensure there's no age field
columns = self.column_classes(Author)
self.assertNotIn("age", columns)
# Add some rows of data
Author.objects.create(name="Andrew", height=30)
Author.objects.create(name="Andrea")
# Add a not-null field
new_field = CharField(max_length=30, default="Godwin")
new_field.set_attributes_from_name("surname")
with connection.schema_editor() as editor:
editor.add_field(
Author,
new_field,
)
# Ensure the field is right afterwards
columns = self.column_classes(Author)
self.assertEqual(columns['surname'][0], "CharField")
self.assertEqual(columns['surname'][1][6], False)
def test_alter(self):
"""
Tests simple altering of fields