#30011: Count with filter annotation bug on filter
-------------------------------------+-------------------------------------
     Reporter:  Taqi Abbas           |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  2.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  Annotation, Count,   |             Triage Stage:
  Filter                             |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Hello Taqi,

 I see that somehow your `COUNT` annotations get their expressions
 duplicated when a subquery is added. This is probably an issue with
 expression cloning when you create `filtered_queryset` from `queryset`.

 One thing that would tremendously help debugging this further is if you
 could reduce your reproduction case to a minimal set of conditions by
 progressively removing annotations and simplifying your querysets. Right
 now there's a lot of things involved that makes your query massive and
 hard to pinpoint the exact source of the failure.

 To get you started, does the following crash as well?

 {{{#!python
 queryset = Ticket.objects.annotate(
     commit_count=Count('commit',
 filter=models.Q(commit__is_deleted=False))
 )
 filtered_queryset = queryset.filter(Q(heading__icontains=''))
 print(queryset.filter(id__in=filtered_queryset.values('id')).query)
 }}}

 By the way, you probably haven't noticed it yet but your queryset is
 probably not returning the right results for these annotations because
 multiple tables are the cross joined. This is explained in the
 [https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#combining-
 multiple-aggregations documentation] and tracked in #10060. This is a
 limitation that #28296 ought to fix by eventually providing a `subquery`
 argument to `Count`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30011#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/065.f5770cf28eedd097072140cc51c68282%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to