#24662: Sum() returns True/False when used with BooleanField & MySQL
----------------------------------------------+--------------------
     Reporter:  chriskief                     |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 When using Sum() on a BooleanField when only 1 record is present, the ORM
 returns True/False rather than 1/0. I've tested with MySQL and SQLite,
 only MySQL exhibits the bug.

 This is a change in behavior from previous versions of Django where a
 Decimal would be returned.

 Environment:
 MySQL - 5.6.24
 MySQL-python==1.2.5

 Code to reproduce (new project / app):

 {{{
 class Example(models.Model):
     foo = models.BooleanField(default=True)
 }}}

 Simple query to demonstrate the difference:

 {{{
 # add a single row
 a = Example(foo=True)
 a.save()

 # query
 Example.objects.all().aggregate(count=Count('foo'), sum=Sum('foo'))

 # results
 # notice Django 1.8 + MySQL
 Django 1.7.7 + MySQL
 {'count': 1, 'sum': Decimal('1')}

 Django 1.7.7 + SQLite
 {'count': 1, 'sum': 1}

 Django 1.8 + MySQL
 {'count': 1, 'sum': True}

 Django 1.8 + SQLite
 {'count': 1, 'sum': 1}

 # add a second row
 a = Example(foo=True)
 a.save()

 # query
 Example.objects.all().aggregate(count=Count('foo'), sum=Sum('foo'))

 # results
 # notice Django 1.8 + MySQL now returns a decimal
 Django 1.7.7 + MySQL
 {'count': 2, 'sum': Decimal('2')}

 Django 1.7.7 + SQLite
 {'count': 2, 'sum': 2}

 Django 1.8 + MySQL
 {'count': 2, 'sum': Decimal('2')}

 Django 1.8 + SQLite
 {'count': 2, 'sum': 2}
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24662>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.6ec41b768701c64c27068e50ae314c08%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to