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 Simon Charette, 9 years ago

Triage Stage: UnreviewedAccepted
Version: 1.8master

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.

comment:2 by MarshalSHI, 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 Mariusz Felisiak, 8 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:4 by Mariusz Felisiak, 8 years ago

Has patch: set

comment:5 by Simon Charette, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by GitHub <noreply@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 1d070d02:

Fixed #25414 -- Fixed QuerySet.annotate() with pk in values() on MySQL.

Thanks Tim Graham and Simon Charette for the reviews.

Note: See TracTickets for help on using tickets.
Back to Top