#34358 closed Bug (fixed)
qs.filter(Exact(expr, value)) doesn’t work on aggregations.
| Reported by: | Roman Odaisky | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 4.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
class Child(Model):
parent = ForeignKey(Parent)
a = Parent.objects.filter(Exact(Count("child"), 0)).count()
b = Parent.objects.annotate(n=Count("child")).filter(n=0).count()
Expected: a == b == <number of childless parents>
Actual: a is always 0, b is correct
The two result in different SQL:
-- a:
SELECT COUNT(*) AS "__count"
FROM "parent" LEFT OUTER JOIN "child" ON ("parent"."id" = "child"."report_id")
HAVING COUNT("child"."id") = 0
-- b:
SELECT COUNT(*)
FROM (
SELECT COUNT("child"."id") AS "n"
FROM "parent" LEFT OUTER JOIN "child" ON ("parent"."id" = "child"."parent_id")
GROUP BY "parent"."id"
HAVING COUNT("child"."id") = 0
)
Am I correct in assuming A should have worked as well?
Change History (3)
comment:1 by , 3 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:2 by , 3 years ago
| Summary: | qs.filter(Exact(expr, value)) doesn’t work → qs.filter(Exact(expr, value)) doesn’t work on aggregations. |
|---|
Note:
See TracTickets
for help on using tickets.
The issue appears to be fixed in Django 4.2 by 59bea9efd2768102fc9d3aedda469502c218e9b7 (#28477) which strips unused annotation.
Prior to this change the generate SQL was simply wrong as
HAVINGcannot be used without aGROUP BYPlease test against 4.2b1 and confirm if your issue is fixed or not.