From 2a30b39f40265c9228fc2e706bcb6e1df1aae991 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 25 Jan 2014 00:09:56 +0000 Subject: [PATCH] Fixed #21783: More SQLite default fun with nulls. --- django/db/backends/sqlite3/base.py | 6 +++--- django/db/backends/sqlite3/schema.py | 2 +- tests/schema/tests.py | 28 +++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index cf0326db27..3c8f170b7d 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -223,12 +223,12 @@ class DatabaseOperations(BaseDatabaseOperations): except _sqlite3.ProgrammingError: pass # Manual emulation of SQLite parameter quoting - if isinstance(value, six.integer_types): + if isinstance(value, type(True)): + return str(int(value)) + elif isinstance(value, six.integer_types): return str(value) elif isinstance(value, six.string_types): return '"%s"' % six.text_type(value) - elif isinstance(value, type(True)): - return str(int(value)) elif value is None: return "NULL" else: diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 1ad5bfc17b..cd80d457c6 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -30,7 +30,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): for field in create_fields: body[field.name] = field # If there's a default, insert it into the copy map - if field.get_default(): + if field.has_default(): mapping[field.column] = self.connection.ops.quote_parameter( field.get_default() ) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 88388ac0b4..99805c45c7 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -4,7 +4,7 @@ import unittest from django.test import TransactionTestCase from django.db import connection, DatabaseError, IntegrityError, OperationalError -from django.db.models.fields import IntegerField, TextField, CharField, SlugField +from django.db.models.fields import IntegerField, TextField, CharField, SlugField, BooleanField from django.db.models.fields.related import ManyToManyField, ForeignKey from django.db.transaction import atomic from .models import (Author, AuthorWithM2M, Book, BookWithLongName, @@ -185,6 +185,32 @@ class SchemaTests(TransactionTestCase): self.assertEqual(columns['surname'][1][6], connection.features.interprets_empty_strings_as_nulls) + def test_add_field_temp_default_boolean(self): + """ + Tests adding fields to models with a temporary default where + the default is False. (#21783) + """ + # 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 = BooleanField(default=False) + new_field.set_attributes_from_name("awesome") + 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['awesome'][0], "BooleanField") + def test_alter(self): """ Tests simple altering of fields