﻿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
31251	QuerySet.annotate() crashes when grouping by OuterRef().	Oliver Ford	Rohit Jha	"I'm trying to construct a query like:
{{{
cheapest_query = models.Subquery(qs.annotate(
    groupbytype=models.OuterRef(""type""),
).values(""groupbytype"")).annotate(
    cheapest=models.Min(""price""),
).values(""cheapest""))

qs = qs.annotate(premium_over_cheapest=models.F(""price"") - cheapest_query)
}}}

However this fails in the second `annotate` (`cheapest=`) since `ResolvedOuterRef` does not descend from `BaseExpression`; so it has no `get_group_by_cols` method:
{{{
   File ""/usr/local/lib/python3.8/site-packages/django/db/models/query.py"", line 1078, in annotate
     clone.query.set_group_by()
  File ""/usr/local/lib/python3.8/site-packages/django/db/models/sql/query.py"", line 1938, in set_group_by
     inspect.getcallargs(annotation.get_group_by_cols, alias=alias)
 AttributeError: 'ResolvedOuterRef' object has no attribute 'get_group_by_cols'
}}}

https://github.com/django/django/blob/master/django/db/models/expressions.py"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed	db,outerref,group by,subquery,annotate	Rohit Jha	Ready for checkin	1	0	0	0	1	0
