﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23836	aggregate with a sliced queryset doesn't work with relations	Reinout van Rees	nobody	"#12886 fixed aggregation for sliced querysets by using subqueries.

This also fixed the GIS ""Extent"" aggregation for sliced querysets, which was reported in #15101. 

While investigating #15101, a test we made failed initially. We noticed however that we were doing an extent aggregation over a relation. So we also made a test for the simple slice+extent() case, which worked.

Aha! Then we made a test similar to the one made to test #12886, but which followed a relation. At least with postgres this resulted in an error: ```column subquery.num_awards does not exist```.

{{{
Traceback (most recent call last):
  File ""/d/dev/repos/django/django/tests/aggregation/tests.py"", line 664, in test_aggregation_with_limit_and_relation
    vals = qs.aggregate(average_num_awards=Avg('publisher__num_awards'))['average_num_awards']
  File ""/d/dev/repos/django/django/django/db/models/query.py"", line 342, in aggregate
    return query.get_aggregation(using=self.db, force_subq=force_subq)
  File ""/d/dev/repos/django/django/django/db/models/sql/query.py"", line 366, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File ""/d/dev/repos/django/django/django/db/models/sql/compiler.py"", line 817, in execute_sql
    cursor.execute(sql, params)
  File ""/d/dev/repos/django/django/django/db/backends/utils.py"", line 65, in execute
    return self.cursor.execute(sql, params)
  File ""/d/dev/repos/django/django/django/db/utils.py"", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ""/d/dev/repos/django/django/django/db/backends/utils.py"", line 65, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: column subquery.num_awards does not exist
LINE 1: SELECT AVG(""subquery"".""num_awards"") FROM (SELECT ""aggregatio...
}}}

I don't know how bad this is. Whether it is even supposed to work.

I also don't know how hard to fix it is :-) Might be just a matter of doing ```AVG(""subquery"".""publisher"".""num_awards"")```, so adding the ```.publisher``` relation name in there.

Pull request with the failing test is upcoming."	Bug	new	Database layer (models, ORM)	dev	Normal			Sander Smits	Unreviewed	0	0	0	0	0	0
