Opened 9 years ago
Closed 8 years ago
#25414 closed Bug (fixed)
invalid annotate query for mysql when primary key is included
Reported by: | Sander Niemeijer | Owned by: | Mariusz Felisiak |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | mysql, annotate |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using the bookstore example from https://docs.djangoproject.com/en/1.8/topics/db/aggregation/ try:
print Publisher.objects.values('name', 'book__rating').annotate(total=Sum('book__rating')).query print Publisher.objects.values('id', 'book__rating').annotate(total=Sum('book__rating')).query
For sqlite and postgresql this gives:
SELECT "bookstore_publisher"."name", "bookstore_book"."rating", SUM("bookstore_book"."rating") AS "total" FROM "bookstore_publisher" LEFT OUTER JOIN "bookstore_book" ON ( "bookstore_publisher"."id" = "bookstore_book"."publisher_id" ) GROUP BY "bookstore_publisher"."name", "bookstore_book"."rating" SELECT "bookstore_publisher"."id", "bookstore_book"."rating", SUM("bookstore_book"."rating") AS "total" FROM "bookstore_publisher" LEFT OUTER JOIN "bookstore_book" ON ( "bookstore_publisher"."id" = "bookstore_book"."publisher_id" ) GROUP BY "bookstore_publisher"."id", "bookstore_book"."rating"
but for mysql this gives:
SELECT `bookstore_publisher`.`name`, `bookstore_book`.`rating`, SUM(`bookstore_book`.`rating`) AS `total` FROM `bookstore_publisher` LEFT OUTER JOIN `bookstore_book` ON ( `bookstore_publisher`.`id` = `bookstore_book`.`publisher_id` ) GROUP BY `bookstore_publisher`.`name`, `bookstore_book`.`rating` ORDER BY NULL SELECT `bookstore_publisher`.`id`, `bookstore_book`.`rating`, SUM(`bookstore_book`.`rating`) AS `total` FROM `bookstore_publisher` LEFT OUTER JOIN `bookstore_book` ON ( `bookstore_publisher`.`id` = `bookstore_book`.`publisher_id` ) GROUP BY `bookstore_publisher`.`id` ORDER BY NULL
The `bookstore_book`.`rating`
is missing from the GROUP BY
if the primary key `id`
of the publisher is included in the 'values' list.
Change History (6)
comment:1 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Version: | 1.8 → master |
comment:2 by , 8 years ago
Is there any updates for this bug? I got same GROUP BY
with id
only in multi-values clause (using Django 1.8.17).
comment:3 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 8 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Note:
See TracTickets
for help on using tickets.
I didn't reproduce but by looking at collapse_group_by()'s MySQL special casing I can see how this is an issue.
On MySQL the existing code collapse the
GROUP BY
clause to the queryset model's primary key if it's present. It should also account for expressions referring to aliases other than the initial table just like the branch for PostgreSQL does.