#30203: Union with group by don't generate correct Subquery -------------------------------------+------------------------------------- Reporter: Nikolas | Owner: nobody Type: Bug | Status: new 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 | -------------------------------------+------------------------------------- if dont use union - all fine, generated SQL:
{{{ SELECT "service_transaction"."phone_number_id", "rate"."service_id", COUNT("rate"."service_id") AS "rate__service_id__count" FROM "service_transaction" INNER JOIN "phone_number" ON ("service_transaction"."phone_number_id" = "phone_number"."id") INNER JOIN "rate" ON ("service_transaction"."rate_id" = "rate"."id") WHERE ("phone_number"."deactivated" IS NULL AND ("service_transaction"."sms_last_id" IS NOT NULL OR "service_transaction"."deactivated" > (STATEMENT_TIMESTAMP())) AND "rate"."country_id" = 2) GROUP BY "service_transaction"."phone_number_id", "rate"."service_id" }}} but if before use union get: {{{ (SELECT "service_transaction"."phone_number_id", "rate"."service_id" FROM "service_transaction" INNER JOIN "phone_number" ON ("service_transaction"."phone_number_id" = "phone_number"."id") INNER JOIN "rate" ON ("service_transaction"."rate_id" = "rate"."id") WHERE ("phone_number"."deactivated" IS NULL AND ("service_transaction"."sms_last_id" IS NOT NULL OR "service_transaction"."deactivated" > (STATEMENT_TIMESTAMP())) AND "rate"."country_id" = 2)) UNION (SELECT "banned_phone_number"."phone_number_id", "banned_phone_number"."service_id" FROM "banned_phone_number" INNER JOIN "phone_number" ON ("banned_phone_number"."phone_number_id" = "phone_number"."id") WHERE ("phone_number"."deactivated" IS NULL AND "banned_phone_number"."user_id" = 4)) }}} my code very simple: {{{ active_service_transaction = active_service_transaction.union(activate_banned_phone_numbers) # all fine if dont use union . . . active_service_transaction = active_service_transaction.annotate(Count('rate__service_id')) }}} i think this is bug, if not bug must generate exception th, sorry my poor English -- Ticket URL: <https://code.djangoproject.com/ticket/30203> 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.938b4ab877c4f60a661d30ffa06c4d27%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.