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?
--
Curtis