#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 Taqi Abbas):

 Hey Simon,

 Your suggested code also breaks. This is the line that invalidates the

 As for the cross joins issue, this is the code that I basically use to
 remove duplicates.
 if hasattr(queryset.model, 'id'):
    return queryset.filter(id__in=filtered_queryset.values('id'))
    return filtered_queryset.distinct(*view.get_ordering(request))
 its done like this because distinct fails for a model that has a jsonb
 field. So I just do this.

 I have made a github repo with a testcase to reproduce it. Heres the

 This a simpler query reproduced from the repo above, hope this helps.

 queryset = Ticket.objects.filter(is_deleted=False)\
    .annotate(commit_count=Count('commit_set', filter=Q(is_deleted=False)))
 filtered_queryset = queryset.filter(heading__icontains='')
 result = queryset.filter(id__in=filtered_queryset.values('id'))
 "test_count_ticket"."is_deleted" = false, "test_count_ticket"."is_deleted"
 = false) filter (WHERE "test_count_ticket"."is_deleted" = false) AS
 FROM            "test_count_ticket"
 LEFT OUTER JOIN "test_count_commit"
 ON              (
                                 "test_count_ticket"."id" =
 WHERE           (
                                 "test_count_ticket"."is_deleted" = false
                 AND             "test_count_ticket"."id" IN
                                                 SELECT          u0."id"
 "test_count_ticket" u0
                                                 LEFT OUTER JOIN
 "test_count_commit" u1
                                                 ON              (
 u0."id" = u1."ticket_id")
                                                 WHERE           (
 u0."is_deleted" = false
 upper(u0."heading"::text) LIKE upper(%%))
                                                 GROUP BY        u0."id"))
 GROUP BY        "test_count_ticket"."id"

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

Reply via email to