Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21783: More SQLite default fun with nulls.

  • Loading branch information...
commit 2a30b39f40265c9228fc2e706bcb6e1df1aae991 1 parent 06bd181
@andrewgodwin andrewgodwin authored
View
6 django/db/backends/sqlite3/base.py
@@ -223,12 +223,12 @@ def quote_parameter(self, value):
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
2  django/db/backends/sqlite3/schema.py
@@ -30,7 +30,7 @@ def _remake_table(self, model, create_fields=[], delete_fields=[], alter_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():
+ if field.has_default():
mapping[field.column] = self.connection.ops.quote_parameter(
field.get_default()
)
View
28 tests/schema/tests.py
@@ -4,7 +4,7 @@
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 @@ def test_add_field_temp_default(self):
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
Please sign in to comment.
Something went wrong with that request. Please try again.