#24662: Sum() returns True/False when used with BooleanField & MySQL
-------------------------------------+-------------------------------------
     Reporter:  chriskief            |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by chriskief):

 * needs_docs:   => 0
 * needs_tests:   => 0
 * needs_better_patch:   => 0


Old description:

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

New description:

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/24662#comment:1>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.4a94e631d48d5a1e9f6061c92327751d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to