#10060: Multiple table annotation failure
-------------------------------------+-------------------------------------
Reporter: svsharma@… | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Matthijs Kooijman):
For anyone else running into this, a reasonable workaround seems to be to
use subqueries for aggregating annotations. This is a bit verbose/hacky in
Django currently, as shown by the multitude of approaches in the
stackoverflow link from [[comment:69|Antoine's comment]]. However, I've
successfully used the [[https://github.com/martsberger/django-sql-utils
|django-sql-utils]] package for this just now. That sounds a bit bulky,
but it just has two utilities, one of which is a `SubqueryAggregate` class
(with derived `SubqueryCount`, `SubquerySum`, etc.) that make converting a
regular joining aggregate into a subquery aggregate easy and concise,
without changing the structure much.
For example taking the example from [[comment:66|comment 66]] and
converting the second annotation to a subquery with django-sql-utils,
you'd get:
{{{
Branch.objects.annotate(
total=Sum('center__client__loan__amount'),
repaid=SubquerySum('center__client__loan__payment_schedule__payments__principal'),
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/10060#comment:71>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/081.f2bd649eb8b03b75ac8a135cbef0b98f%40djangoproject.com.