summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2019-12-21 23:22:49 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-23 09:24:37 +0100
commit7b065c41e406fb22d39d1f7b4cfed96bf06dc591 (patch)
treecb27aed8fbce0cf6d0a14c0239538e6d570144e4 /django/db/models/sql/query.py
parentb1a309f6f08bc27c7d843b7be493282a2b0ac8fe (diff)
[3.0.x] Fixed #31109 -- Disabled grouping by aliases on QuerySet.exists().
Clearing the SELECT clause in Query.has_results was orphaning GROUP BY references to it. Thanks Thierry Bastian for the report and Baptiste Mispelon for the bisect. Regression in fb3f034f1c63160c0ff13c609acd01c18be12f80. Backport of 720de4d0441fcfdb543051389c70efbe66ed962a from master
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index cc1d4b1d38..7429939cf9 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -527,7 +527,9 @@ class Query(BaseExpression):
if not q.distinct:
if q.group_by is True:
q.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
- q.set_group_by()
+ # Disable GROUP BY aliases to avoid orphaning references to the
+ # SELECT clause which is about to be cleared.
+ q.set_group_by(allow_aliases=False)
q.clear_select_clause()
q.clear_ordering(True)
q.set_limits(high=1)
@@ -1920,7 +1922,7 @@ class Query(BaseExpression):
if force_empty:
self.default_ordering = False
- def set_group_by(self):
+ def set_group_by(self, allow_aliases=True):
"""
Expand the GROUP BY clause required by the query.
@@ -1943,6 +1945,8 @@ class Query(BaseExpression):
warnings.warn(msg, category=RemovedInDjango40Warning)
group_by_cols = annotation.get_group_by_cols()
else:
+ if not allow_aliases:
+ alias = None
group_by_cols = annotation.get_group_by_cols(alias=alias)
group_by.extend(group_by_cols)
self.group_by = tuple(group_by)