﻿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
34145	Explicit GROUPing by aggregate is not supported	Karolis Ryselis	nobody	"Consider the following models:
{{{#!python
class PackingTag(models.Model):
    title = models.CharField(max_length=64)


class Packing(models.Model):
    tags = models.ManyToManyField(to='PackingTag')


class PackingItem(models.Model):
    packing = models.ForeignKey(to='Packing', on_delete=models.CASCADE)
    amount = models.DecimalField(max_digits=12, decimal_places=4)
}}}

Consider a queryset like this: 

{{{#!python
PackingItem.objects.annotate(tags=Min('packing__tags__title')).values('tags').annotate(total_amount=Sum('amount'))
}}}

Currently on both 4.1 and 3.2 it yields this query using SQLite backend (MySQL backend on 3.2 outputs a very similar query as well):

{{{
SELECT MIN(""packings_packingtag"".""title"") AS ""tags"", CAST(SUM(""packings_packingitem"".""amount"") AS NUMERIC) AS ""total_amount""
FROM ""packings_packingitem""
         INNER JOIN ""packings_packing"" ON (""packings_packingitem"".""packing_id"" = ""packings_packing"".""id"")
         LEFT OUTER JOIN ""packings_packing_tags"" ON (""packings_packing"".""id"" = ""packings_packing_tags"".""packing_id"")
         LEFT OUTER JOIN ""packings_packingtag"" ON (""packings_packing_tags"".""packingtag_id"" = ""packings_packingtag"".""id"")
}}}

I would expect this queryset to either group by tags (this example may not make much sense, my use case involves GroupConcat aggregate from django_mysql package, however, this is the same for all aggregates) because passing a regular field expression constructs a group by in the SQL, or crash with an error telling that grouping by annotations is not supported."	Bug	closed	Database layer (models, ORM)	4.1	Normal	needsinfo		Simon Charette	Unreviewed	0	0	0	0	0	0
