﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32126	Empty Case() annotation crashes when used with GROUP BY	Hannes Ljungberg	Hannes Ljungberg	"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."	Bug	closed	Database layer (models, ORM)	3.1	Normal	fixed			Ready for checkin	1	0	0	0	0	0
