On Saturday, January 9, 2016 at 7:01:38 AM UTC+2, Curtis Maloney wrote:
>
> Hey all,
>
> I'm trying to write a PR for adding Postgres9.4's "FILTER" syntax for
> aggregates, but I've hit a bug I can't figure how to fix [and nobody's
> awake on IRC :)]
>
> https://github.com/django/django/pull/5956
>
> Basically, for the query:
>
> qset = FilterAggParentModel.objects.annotate(
> zero_count=FilterAgg(Count('filteraggtestmodel'),
> Q(filteraggtestmodel__integer_field=1)),
> child_count=Count('filteraggtestmodel'),
> )
>
> It generates the SQL:
>
> SELECT "postgres_tests_filteraggparentmodel"."id",
> COUNT("postgres_tests_filteraggtestmodel"."id") FILTER(WHERE
> "postgres_tests_filteraggtestmodel"."integer_field" = 0) AS "zero_count",
> COUNT("postgres_tests_filteraggtestmodel"."id") AS "child_count"
> FROM "postgres_tests_filteraggparentmodel"
> LEFT OUTER JOIN "postgres_tests_filteraggtestmodel" ON
> ("postgres_tests_filteraggparentmodel"."id" =
> "postgres_tests_filteraggtestmodel"."parent_id")
> GROUP BY "postgres_tests_filteraggparentmodel"."id",
> "postgres_tests_filteraggtestmodel"."integer_field"
>
>
> The problem being that
> "postgres_tests_filteraggtestmodel"."integer_field" is included in the
> GROUP BY clause...
>
> Does anyone who understands this part of the ORM have a suggestion for
> how to fix this?
>
If you don't return anything from get_group_by_cols() no part of the
expression should be added to the GROUP BY clause. I wonder why the
integer_field is added to the group by, a bug somewhere in compiler maybe?
- Anssi
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/980bd68a-f75c-4d64-a18f-58420bc45859%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.