{{{
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25414>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_docs: => 0
* needs_better_patch: => 0
* version: 1.8 => master
* needs_tests: => 0
* stage: Unreviewed => Accepted
Comment:
I didn't reproduce but by looking at
[https://github.com/django/django/blob/233b46f93171d4a7cc279bc3f35e5a99e9a167b8/django/db/models/sql/compiler.py#L136-L154
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:1>
Comment (by MarshalSHI):
Is there any updates for this bug? I got same `GROUP BY` with `id` only in
multi-values clause (using Django 1.8.17).
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:2>
* status: new => assigned
* owner: nobody => felixxm
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:3>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/8258 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"1d070d027c218285b66c0bde8079034b33a87f11" 1d070d02]:
{{{
#!CommitTicketReference repository=""
revision="1d070d027c218285b66c0bde8079034b33a87f11"
Fixed #25414 -- Fixed QuerySet.annotate() with pk in values() on MySQL.
Thanks Tim Graham and Simon Charette for the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25414#comment:6>