#23877: Aggregation over subquery doesn't ensure the subquery has needed columns
-------------------------------------+-------------------------------------
Reporter: akaariai | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: master
layer (models, ORM) | Keywords:
Severity: Normal | Has patch: 1
Triage Stage: Accepted | Needs tests: 0
Needs documentation: 0 | Easy pickings: 0
Patch needs improvement: 1 |
UI/UX: 0 |
-------------------------------------+-------------------------------------
When aggregating over a subquery (result of `.annotate().aggregate()`
queries for example), then Django doesn't ensure the subquery has the
target of the aggregation included in the subquery's SELECT clause.
An example query is
{{{
Book.objects.annotate(avg_price=Avg('price')).aggregate(
publisher_awards=Sum('publisher__num_awards')
)
}}}
Here, the `Sum('publisher__num_awards')` creates correctly a join to
publisher, but the num_awards column of the publisher table is not
selected in the generated subquery. As the column is not present in the
subquery, the database generates an error for the
`SUM("subquery"."num_awards") FROM (...) subquery` SQL generated by
Django.
To fix this we need to make sure that aggregations over a subquery has all
the base columns it needs added to the inner query.
This bug seems to have existed always in Django. I'll mark as master as
that is where this should be fixed.
--
Ticket URL: <https://code.djangoproject.com/ticket/23877>
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/051.5d9ce43a945b2ed61ed16c1caab6bccc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.