#28296: Subquery with annotation fails without tweaking
-------------------------------------+-------------------------------------
     Reporter:  László Károlyi       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Hello László,

 It's not clear to me why you not expecting a `GROUP BY` statement to be
 present in the subquery when you are explicitly grouping by `id` by using
 `values('id')` before annotating. Please see the section about aggregation
 and the usage of `values()` to `GROUP BY` in the
 [https://docs.djangoproject.com/en/1.11/topics/db/aggregation/#order-of-
 annotate-and-values-clauses aggregation documentation].

 Also, is there a reason you are not simply using
 [https://docs.djangoproject.com/en/1.11/ref/models/conditional-expressions
 /#conditional-aggregation conditional aggregation] for this query instead?

 {{{#!python
 Performance.objects.annotate(
     reserved_seats=Count(
         Case(When(seat_reservations__status__in=TAKEN_TYPES,
 then='seat_reservations')),
     ),
 )
 }}}

 Note that I'm assuming `SeatReservation.performance.related_name ==
 'seat_reservations'` above.

--
Ticket URL: <https://code.djangoproject.com/ticket/28296#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.6fe24a99b3afe9632d58bb826338f9ef%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to