Combining multiple aggregations with annotate()

75 views
Skip to first unread message

Yonas

unread,
Jan 5, 2022, 9:13:17 AM1/5/22
to Django developers (Contributions to Django itself)

Hello,

There's a ticket opened 13 years ago explaining a problem with combining multiple aggregations with annotate(). And the solution appears to be documenting the problem.

But for people skimming through the documentation, the message might not be noticeable. Showing the problem in a warning message could help draw attention better. It's used here and in other places in the doc.

In addition to documenting the problem, raising an exception might prevent developers from spending hours trying to debug their code.

While the problem is recognized, there's an example in the documentation that shows the usage of multiple aggregations.

Niccolò Mineo

unread,
Jan 5, 2022, 9:16:43 AM1/5/22
to Django developers (Contributions to Django itself)
I would be in favor of a real time information about the issue.

Kye Russell

unread,
Jan 5, 2022, 9:24:55 AM1/5/22
to django-d...@googlegroups.com
I haven’t looked at the current warning in a while but I do remember thinking that it could be more prominent. 

Kye

On 5 Jan 2022, at 10:16 pm, Niccolò Mineo <niccol...@gmail.com> wrote:

I would be in favor of a real time information about the issue.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1e17088d-dd5f-481e-94c7-2b4d2bcfa91dn%40googlegroups.com.

Yonas

unread,
Jan 5, 2022, 9:59:24 AM1/5/22
to Django developers (Contributions to Django itself)
In that case, at least there should be a warning message in the documentation. And what do you think of the example? Isn't it contradicting the documentation?

charettes

unread,
Jan 5, 2022, 11:44:11 AM1/5/22
to Django developers (Contributions to Django itself)
While addressing the issue is complex and will require quite a bit of work to solve in a backward-compatible way I believe that it should be feasible to emit a MultipleMultiValuedJoin(RuntimeWarning) with pointers for alternatives (e.g. using subqueries) without too much hassle when aggregation is performed and more than one multi-valued relationship is involved.

The docs you pointed at mention the multiple aggregation problem but a similar thing happens when filtering against a multi-valued relationship after a single aggregation[0] so the problem can be generalized to any form of aggregation on a query that joins more than one multi-valued relationship (n-to-many).

Simon

Yonas

unread,
Nov 27, 2022, 12:27:02 PM11/27/22
to Django developers (Contributions to Django itself)
Hi Simon,

In addition to what's documented, that's another way annotate() gives the wrong result.

There are a lot of duplicates of #10060, and there are a lot of people who run into this issue.

Given the age of the ticket and the lack of a simple backward compatible solution, what would be the way forward? Maybe breaking changes? Or updating the docs to show a safe usage of annotate()? Or throwing an error?
Reply all
Reply to author
Forward
0 new messages