Opened 10 years ago

Last modified 2 years ago

#24662 closed Bug

Sum() returns True/False when used with BooleanField & MySQL — at Version 1

Reported by: Chris Kief Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: josh.smeaton@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Chris Kief)

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

Change History (1)

comment:1 by Chris Kief, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top