#32126: Empty Case() annotation crashes when used with GROUP BY
-------------------------------------+-------------------------------------
               Reporter:  Hannes     |          Owner:  Hannes Ljungberg
  Ljungberg                          |
                   Type:  Bug        |         Status:  assigned
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Annotating a queryset with a `Case` without any cases will generate an
 invalid query when used with a `GROUP BY`.

 Example:

 {{{
 qs = (
     CaseTestModel.objects.values('string')
     .annotate(test=Case(output_field=IntegerField()),
 integer_sum=Sum('integer'))
     .order_by('string')
 )
 }}}

 Executing this queryset generates the following SQL:

 {{{
 SELECT "expressions_case_casetestmodel"."string", NULL AS "test",
 SUM("expressions_case_casetestmodel"."integer") AS "integer_sum" FROM
 "expressions_case_casetestmodel" GROUP BY
 "expressions_case_casetestmodel"."string", NULL ORDER BY
 "expressions_case_casetestmodel"."string" ASC
 }}}

 Causing the following error:

 {{{
 django.db.utils.ProgrammingError: non-integer constant in GROUP BY
 LINE 1: ...OUP BY "expressions_case_casetestmodel"."string", NULL ORDER...
 }}}

 The  `Case` annotation should be excluded from the `GROUP BY` if it
 contains no cases.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32126>
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/052.e50dacd9e9c5993ddae7a19947c03a53%40djangoproject.com.

Reply via email to