#24692: QuerySet.extra(select=...) is silently dropped with aggregations
-------------------------------------+-------------------------------------
     Reporter:  jdelic               |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (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 jdelic):

 > Looking at it a bit more, though, I noticed that period doesn't seem to
 be used in the values or in the annotate. Do you have a typo there?

 No, not a typo per se. Maybe a mistake, though. I'm basing this off the
 {{{is_recent}}} example in
 https://docs.djangoproject.com/en/1.8/ref/models/querysets/#extra, which
 also doesn't mention the field anywhere else. It might be worth mentioning
 that if you add {{{.values('service_id', 'period')}}} then the QuerySet
 will generate invalid SQL:

 {{{
 #!python
 >>> qs = ServiceAction.objects.extra(select={'period':
 
'(MAX("dbtest_serviceaction"."performed")-MIN("dbtest_serviceaction"."performed"))'}).values('service_id',
 'period').annotate(first_action=Min('performed'),
 last_action=Max('performed'))
 >>> print(qs.query)
 SELECT
 
((MAX("dbtest_serviceaction"."performed")-MIN("dbtest_serviceaction"."performed")))
 AS "period", "dbtest_serviceaction"."service_id",
 MAX("dbtest_serviceaction"."performed") AS "last_action",
 MIN("dbtest_serviceaction"."performed") AS "first_action" FROM
 "dbtest_serviceaction" GROUP BY "dbtest_serviceaction"."service_id",
 
((MAX("dbtest_serviceaction"."performed")-MIN("dbtest_serviceaction"."performed")))
 >>> qs
 ...
 OperationalError: aggregate functions are not allowed in the GROUP BY
 clause
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24692#comment:7>
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 post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.d7e80c38ce0f03681347cf688c7b7e35%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to