Fixed #21783: More SQLite default fun with nulls.

This commit is contained in:
Andrew Godwin 2014-01-25 00:09:56 +00:00
parent 06bd181f97
commit 2a30b39f40
3 changed files with 31 additions and 5 deletions

View File

@ -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:

View File

@ -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()
)

View File

@ -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