#34145: It is currently not possible to group by annotated fields
-------------------------------------------+------------------------
               Reporter:  Karolis Ryselis  |          Owner:  nobody
                   Type:  Bug              |         Status:  new
              Component:  Uncategorized    |        Version:  4.1
               Severity:  Normal           |       Keywords:
           Triage Stage:  Unreviewed       |      Has patch:  0
    Needs documentation:  0                |    Needs tests:  0
Patch needs improvement:  0                |  Easy pickings:  0
                  UI/UX:  0                |
-------------------------------------------+------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34145>
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/01070184572c9fbf-5429a551-e258-4b4b-9a48-d05a3b83daf9-000000%40eu-central-1.amazonses.com.

Reply via email to