#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.
--
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/051.60f068b92cca2ddedded7a5e3e826809%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.