#32006: QuerySet with values with annotate generates wrong SQL query on
PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Konstantin Popov | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: QuerySet values | Triage Stage:
annotate GROUP BY | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Konstantin Popov):
But this does not work properly! I receive error from postgresql. It says
other fields should be in GROUP BY.
This example query is simplified, the real query includes several joined
tables.
{{{
SELECT "rk7data_globalshift"."shiftdate" AS "date",
"rk7data_restaurant"."name" AS "rest",
"rk7data_printcheck"."check_number"
AS "chck", "rk7data_printcheck"."moment" AS "check_mom",
"rk7data_printcheck"."id" AS "check_id", "rk7data_printcheck"."period" AS
"check_period", COUNT("rk7data_printcheck"."check_number") AS "qnt",
SUM("rk7data_printcheck"."binded_sum") AS "pay" FROM "rk7data_printcheck"
INNER JOIN "rk7data_order" ON ("rk7data_printcheck"."order_id" =
"rk7data_order"."id" AND "rk7data_printcheck"."period" =
"rk7data_order"."period") INNER JOIN "rk7data_globalshift" ON
("rk7data_order"."shift_id" = "rk7data_globalshift"."id") INNER JOIN
"rk7data_midserver" ON ("rk7data_globalshift"."midserver_id" =
"rk7data_midserver"."id") INNER JOIN "rk7data_restaurant" ON
("rk7data_midserver"."restaurant_id" = "rk7data_restaurant"."id") WHERE
("rk7data_printcheck"."deleted" = %s AND "rk7data_printcheck"."isbill" =
%s
AND "rk7data_order"."shift_id" IN (SELECT U0."id" FROM
"rk7data_globalshift"
U0 WHERE U0."shiftdate" BETWEEN %s AND %s) AND
"rk7data_printcheck"."period"
IN (%s, %s, %s)) GROUP BY "rk7data_globalshift"."shiftdate",
"rk7data_restaurant"."name", "rk7data_printcheck"."id" ORDER BY "rest"
ASC,
"date" ASC, "chck" ASC
}}}
And the error message is
{{{
ERROR: column "rk7data_printcheck.check_number" must appear in GROUP BY
clause or used in aggregate function
LINE 1: ... AS "date", "rk7data_restaurant". "Name" AS "rest", "rk7data_p
...
}}}
I have found workaround for this, I have wrapped 'id' field with Coalesce
function, but I don't think it is good solution.
Will you reopen this issue now?
--
Ticket URL: <https://code.djangoproject.com/ticket/32006#comment:2>
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/067.0cae14014db380475197f756bfb89001%40djangoproject.com.