#29214: Invalid SQL generated when annotating a subquery with an outerref to an
annotated field.
-------------------------------------+-------------------------------------
     Reporter:  Oskar Persson        |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  queryset             |             Triage Stage:  Accepted
  annotations                        |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Chetan, almost correct. The query will not necessarily involve generating
 an `INNER JOIN` but it happens to do so in this case.

 Most of the logic lives in
 
[https://github.com/django/django/blob/a6b3938afc0204093b5356ade2be30b461a698c5/django/db/models/sql/compiler.py#L1513-L1553
 SQLUpdateCompiler] but the idea is that if the backend supports it the
 whole filtering part of the queryset will be pushed down to a subquery,
 `JOIN`s included, and filtered against using `pk IN`.

 This will be way trickier to implement for subqueries and I'm starting to
 wonder if we didn't get it wrong in the first place by making
 `OuteRef('outerqs__lookup')` result in a new `JOIN` in the outer query
 instead of encapsulating it in the subquery. The way we do it right now
 breaks aggregation in subtle ways #29214 and happens to break update as
 well as we came to discover.

 If the `JOIN` was always encapsulated in the subquery in the first place
 it would solve this ticket, #29214 and I suspect it would make
 implementing #28296 way easier.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29214#comment:14>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.4b155aafa0cc622fa1091a326099e5f3%40djangoproject.com.

Reply via email to