From b6e48f514ebe4e31b76e1750e043d4f296e645dc Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 28 May 2018 00:25:19 +0200 Subject: [2.1.x] Fixed #29416 -- Removed unnecesary subquery from GROUP BY clause on MySQL when using a RawSQL annotation. Regression in 1d070d027c218285b66c0bde8079034b33a87f11. Backport of 4ab1f559e8d1264bcb20bb497988973194f5d8f2 from master --- django/db/models/sql/compiler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'django/db/models/sql/compiler.py') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index f5222f21be..8b0fd1da46 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -157,7 +157,9 @@ class SQLCompiler: } expressions = [pk] + [ expr for expr in expressions - if expr in having or getattr(expr, 'alias', None) not in pk_aliases + if expr in having or ( + getattr(expr, 'alias', None) is not None and expr.alias not in pk_aliases + ) ] elif self.connection.features.allows_group_by_selected_pks: # Filter out all expressions associated with a table's primary key -- cgit v1.3