Hi everyone, First, there was ticket #10461[1] -- a complaint that annotate doesn't work well with generic relations. Then, it was documented[2] as not working. But then, people continued looking at the ticket, and in the end, it was declared as fixed and a test was added[3]. But deeper checking[4] finds that the annotation does not do the intuitive thing -- at least when aggregating over the related objects themselves (rather than fields on them), the field it uses to aggregate on is the relation field (object_id, by default) rather than the pk. This is very odd behavior, as the relation field is guaranteed to be equal on all child records of a given record -- and equal to the pk of the parent record; thus, the only meaningful aggregation is Count().
However, our docs still just claim[5] that aggregations and generic relations do not work together. Which, given the findings, seems to be a good description. So, what should we do? -- Error out on any attempt to aggregate over generic relations. This appears to be the most correct behavior, but will break working code (either Count annotations or code which creates annotations but doesn't actually use them). -- Fix annotations to use the right field? Is it possible? Do specific fields [i.e. Sum('generic_relation__id')] work? (I'll try myself, if nobody beats me to it, but it's rather late for me now) -- Is it possible that annotations work and aggregations don't? Thanks, Shai. [1] https://code.djangoproject.com/ticket/10461 [2] https://github.com/django/django/commit/d987b378ce [3] https://github.com/django/django/commit/76da053641 [4] https://github.com/django/django/commit/e8223b889a#commitcomment-8989832 [5] https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#reverse- generic-relations