[Django] #24835: exists() incorrect after annotation with Count()

25 views
Skip to first unread message

Django

unread,
May 20, 2015, 10:50:47 PM5/20/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
----------------------------------------------+--------------------
Reporter: pteromys | Owner: nobody
Type: Bug | 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
----------------------------------------------+--------------------
exists() is returning wrong answers when I'm filtering on the results of
Count(). Maybe I wanted to know whether any blog post had gotten over 100
comments. The following transcripts are on Django 1.8.1 / Python 2.7.9 /
SQLite. The answer is also wrong on Python 3.4.3 but correct on Django
1.7.4.

== Testcase (see below for setup) ==

Assume at least 2 Users exist in the database.

{{{
>>> User.objects.annotate(c=models.Count('id')).values_list('c')
[(1,), (1,)]
>>> User.objects.annotate(c=models.Count('id')).filter(c__gt=1)
[]
}}}

So far this is to be expected. No user has more than one id--which makes
the following surprising.

{{{
>>> User.objects.annotate(c=models.Count('id')).filter(c__gt=1).exists()
True
}}}

What further raises the suspicion of unintended behavior is that if you
increase the "1" to the number of Users (or higher), then the exists()
starts to return False.

{{{
>>>
User.objects.annotate(c=models.Count('id')).filter(c__gt=User.objects.count()).exists()
False
}}}

== Setup ==

{{{
$ django-admin2 startproject blah
$ cd blah
$ ./manage.py syncdb
...
You have installed Django's auth system, and don't have any superusers
defined.
Would you like to create one now? (yes/no): no
$ ./manage.py shell
>>> from django.contrib.auth.models import User
>>> User.objects.create(username='a')
>>> User.objects.create(username='b')
>>> from django.db import models
}}}

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

Django

unread,
May 22, 2015, 8:10:12 AM5/22/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------

Reporter: pteromys | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* severity: Normal => Release blocker
* needs_tests: => 0
* needs_docs: => 0


Comment:

Bisected regression to 0c7633178fa9410f102e4708cef979b873bccb76. Test for
Django's test suite attached.

--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:1>

Django

unread,
May 22, 2015, 8:10:22 AM5/22/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------

Reporter: pteromys | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* Attachment "24835-test.diff" added.

Django

unread,
May 22, 2015, 9:13:50 PM5/22/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------

Reporter: pteromys | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* cc: josh.smeaton@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:2>

Django

unread,
May 24, 2015, 2:13:38 PM5/24/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: assigned

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by pwmarcz):

* owner: nobody => pwmarcz
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:3>

Django

unread,
May 24, 2015, 2:17:28 PM5/24/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by pwmarcz):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/4706

--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:4>

Django

unread,
May 25, 2015, 11:53:01 AM5/25/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:5>

Django

unread,
May 25, 2015, 8:36:05 PM5/25/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:6>

Django

unread,
May 25, 2015, 8:47:34 PM5/25/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"801a84ae329a24d9adf01b700429fe8f1285b2b8" 801a84ae]:
{{{
#!CommitTicketReference repository=""
revision="801a84ae329a24d9adf01b700429fe8f1285b2b8"
Fixed #24835 -- Fixed QuerySet.exists() after an annotation with Count()

QuerySet.exists() incorrectly handled query.group_by = True
case (grouping by all select fields), causing GROUP BY
expressions to be wiped along with select fields.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:7>

Django

unread,
May 25, 2015, 8:53:27 PM5/25/15
to django-...@googlegroups.com
#24835: exists() incorrect after annotation with Count()
-------------------------------------+-------------------------------------
Reporter: pteromys | Owner: pwmarcz
Type: Bug | Status: closed
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"2aa2b9f291e43bffc07c535dc5906094e6df0bb7" 2aa2b9f]:
{{{
#!CommitTicketReference repository=""
revision="2aa2b9f291e43bffc07c535dc5906094e6df0bb7"
[1.8.x] Fixed #24835 -- Fixed QuerySet.exists() after an annotation with
Count()

QuerySet.exists() incorrectly handled query.group_by = True
case (grouping by all select fields), causing GROUP BY
expressions to be wiped along with select fields.

Backport of 801a84ae329a24d9adf01b700429fe8f1285b2b8 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24835#comment:8>

Reply all
Reply to author
Forward
0 new messages