Skip to content

Commit

Permalink
Fixed #13293 -- Corrected a problem with the MySQL handling of boolea…
Browse files Browse the repository at this point in the history
…n return values when a query has an extra() clause. Thanks to mk for the report and draft patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12939 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Apr 9, 2010
1 parent 056c940 commit e9bbdb3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions django/db/backends/mysql/compiler.py
Expand Up @@ -3,13 +3,13 @@
class SQLCompiler(compiler.SQLCompiler):
def resolve_columns(self, row, fields=()):
values = []
for value, field in map(None, row, fields):
index_extra_select = len(self.query.extra_select.keys())
for value, field in map(None, row[index_extra_select:], fields):
if (field and field.get_internal_type() in ("BooleanField", "NullBooleanField") and
value in (0, 1)):
value = bool(value)
values.append(value)
return tuple(values)

return row[:index_extra_select] + tuple(values)

class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
pass
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/model_fields/models.py
Expand Up @@ -69,6 +69,7 @@ class NullBooleanModel(models.Model):

class BooleanModel(models.Model):
bfield = models.BooleanField()
string = models.CharField(max_length=10, default='abc')

###############################################################################
# ImageField
Expand Down
7 changes: 7 additions & 0 deletions tests/regressiontests/model_fields/tests.py
Expand Up @@ -191,6 +191,13 @@ def test_return_type(self):
self.assertTrue(isinstance(b4.nbfield, bool))
self.assertEqual(b4.nbfield, False)

# http://code.djangoproject.com/ticket/13293
# Verify that when an extra clause exists, the boolean
# conversions are applied with an offset
b5 = BooleanModel.objects.all().extra(
select={'string_length': 'LENGTH(string)'})[0]
self.assertFalse(isinstance(b5.pk, bool))

class ChoicesTests(django.test.TestCase):
def test_choices_and_field_display(self):
"""
Expand Down

0 comments on commit e9bbdb3

Please sign in to comment.