#18414: queryset.exists() returns False when queryset is distinct and sliced
----------------------------------------------+--------------------
     Reporter:  bitrut                        |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.4
     Severity:  Normal                        |   Keywords:  orm
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Consider this:
 {{{
 User.objects.count()
 3
 }}}
 This queryset
 {{{
 User.objects.all()[1:].exists()
 True
 }}}
 produces SQL
 {{{
 SELECT (1) AS "a" FROM "user" LIMIT 1 OFFSET 1
 }}}
 so the result is correct.
 But when you add `distinct()`:
 {{{
 User.objects.distinct()[1:].exists()
 False
 }}}
 the SQL is
 {{{
 SELECT DISTINCT (1) AS "a" FROM "user" LIMIT 1 OFFSET 1
 }}}
 which returns nothing, so the `exists()` returns `False` which is
 incorrect.

 `DISTINCT` narrows results to just one '''1''' and `OFFSET` omits the
 first result and goes to the next one which does not exist.
 It's because `DISTINCT` has the higher priority than `OFFSET` in SQL.

 I don't know the perfect solution. Maybe in the case of using
 `distinct()`, slicing and `exists()` together an exception should be
 thrown. Or the `exists()` function should notice this case and call
 `count()`, but this would lead to unexpected DB usage.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18414>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to