#30099: Filter by Count annotated inside Subquery
-------------------------------------+-------------------------------------
Reporter: MrFus10n | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: subquery annotate | Triage Stage: Accepted
filter |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* cc: Simon Charette (added)
* keywords: Subquery annotate Count filter => subquery annotate filter
* stage: Unreviewed => Accepted
Comment:
I'm pretty sure it's caused by `Subquery.contains_aggregate` returning
`True` when it shouldn't; this attribute shouldn't cross the subquery
boundary. Right now it defaults to `BaseExpression.contains_aggregate`
which is based of
[https://github.com/django/django/blob/573f44d62fe1e87e2c20a74eba5e20ca9ff0ed85/django/db/models/expressions.py#L1046-L1052
the filters of the subquery] and would explain why it's the
`.filter(cnt__gt=3)` that triggers the issue.
Could you try setting `Subquery.contains_aggregate = False` and confirm it
drops the outer `GROUP BY`?
Also, would you be interested in submitting a patch if it does fixes it?
It should consist of setting this attribute and adding a test in
[https://github.com/django/django/blob/master/tests/expressions/tests.py
tests/expressions/tests.py] that uses
[https://github.com/django/django/blob/master/tests/expressions/models.py
the existing models]. Happy to walk you through a review if you are
interested.
--
Ticket URL: <https://code.djangoproject.com/ticket/30099#comment:6>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.f81589d0f2cfe45f097eff503d4aa3f5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.