From e802c97581594e3a37e6497443b105ecb9920a55 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 19 Jan 2014 17:10:24 +0000 Subject: [PATCH] Fixed #21783: Use defaults for adding NOT NULL on sqlite --- django/db/backends/sqlite3/base.py | 2 +- django/db/backends/sqlite3/schema.py | 5 +++++ tests/schema/tests.py | 28 +++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index e55973ea39..cf0326db27 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -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: diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 41391724f6..1ad5bfc17b 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -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] diff --git a/tests/schema/tests.py b/tests/schema/tests.py index e0000e2b9d..6e92d5b784 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -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