Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit e802c97581594e3a37e6497443b105ecb9920a55 1 parent c9de1b4
@andrewgodwin andrewgodwin authored
View
2  django/db/backends/sqlite3/base.py
@@ -226,7 +226,7 @@ def quote_parameter(self, value):
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
5 django/db/backends/sqlite3/schema.py
@@ -29,6 +29,11 @@ def _remake_table(self, model, create_fields=[], delete_fields=[], alter_fields=
# 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
28 tests/schema/tests.py
@@ -145,7 +145,7 @@ def test_add_field(self):
# 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 @@ def test_add_field(self):
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
Please sign in to comment.
Something went wrong with that request. Please try again.