#31251: Unable to annotate a query that has an OuterRef already
-------------------------------------+-------------------------------------
               Reporter:  OJFord     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |       Keywords:  db,outerref,group
               Severity:  Normal     |  by,subquery,annotate
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31251>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.179c80fb413bc6473cc4e5e065a854c0%40djangoproject.com.

Reply via email to