﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24835	exists() incorrect after annotation with Count()	Andrew Geng	Paweł Marczewski	"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
}}}
"	Bug	closed	Database layer (models, ORM)	1.8	Release blocker	fixed		josh.smeaton@…	Ready for checkin	1	0	0	0	0	0
