#30209: Union with group by don't generate correct Subquery -------------------------------------+------------------------------------- Reporter: Nikolas | Owner: nobody Type: | Status: new Uncategorized | Component: Database | Version: 1.11 layer (models, ORM) | Severity: Normal | Keywords: union, group by Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- reopen: https://code.djangoproject.com/ticket/30203
Simplified for example: have simple query {{{ qs1: QuerySet = User.objects.filter(id__gt=10) qs1 = qs1.values('is_active').annotate(id=Count('pk')) }}} generate: {{{ SELECT "suser_customuser"."is_active", COUNT("suser_customuser"."id") AS "id" FROM "suser_customuser" WHERE "suser_customuser"."id" > 10 GROUP BY "suser_customuser"."is_active" }}} all correct, now i wont use union {{{ qs1: QuerySet = User.objects.filter(id__gt=10) qs2: QuerySet = User.objects.filter(id__lt=10) qs1 = qs1.values('is_active').annotate(id=Count('pk')) }}} now get not correct SQL: {{{ (SELECT "suser_customuser"."is_active", "suser_customuser"."id" FROM "suser_customuser" WHERE "suser_customuser"."id" > 10) UNION (SELECT "suser_customuser"."is_active", "suser_customuser"."id" FROM "suser_customuser" WHERE "suser_customuser"."id" < 10) }}} expected: {{{ select * from ((SELECT "suser_customuser"."is_active", "suser_customuser"."id" FROM "suser_customuser" WHERE "suser_customuser"."id" > 10) UNION (SELECT "suser_customuser"."is_active", "suser_customuser"."id" FROM "suser_customuser" WHERE "suser_customuser"."id" < 10)) U0 group by is_active, id }}} i think this is bug, if not why not show any warning or exception? or just say how rewrite query for correct use Subquery screenshot: http://dl4.joxi.net/drive/2019/02/24/0002/0003/143363/63/2eab4a5318.jpg -- Ticket URL: <https://code.djangoproject.com/ticket/30209> 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/054.5dd6037719bb536f58743b25d95292b2%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.