summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-03-19 19:54:36 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-25 09:49:51 +0100
commit72652bcb1b29710d23c3e6f872046d4aedc58665 (patch)
tree0e789a048ed59e44547017f7e0e8e1a15e0ec48b /django/db/models/sql
parent600d7d86931e314837591a227c834803f2b4994b (diff)
[3.0.x] Fixed #31377 -- Disabled grouping by aliases on QuerySet.values()/values_list() when they collide with field names.
Regression in fb3f034f1c63160c0ff13c609acd01c18be12f80. Thanks Holovashchenko Vadym for the report. Backport of 10866a10fe9f0ad3ffdf6f93823aaf4716e6f27c from master
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/query.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index fc1b916812..0e6828b0f1 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1931,6 +1931,19 @@ class Query(BaseExpression):
primary key, and the query would be equivalent, the optimization
will be made automatically.
"""
+ # Column names from JOINs to check collisions with aliases.
+ if allow_aliases:
+ column_names = set()
+ seen_models = set()
+ for join in list(self.alias_map.values())[1:]: # Skip base table.
+ model = join.join_field.related_model
+ if model not in seen_models:
+ column_names.update({
+ field.column
+ for field in model._meta.local_concrete_fields
+ })
+ seen_models.add(model)
+
group_by = list(self.select)
if self.annotation_select:
for alias, annotation in self.annotation_select.items():
@@ -1945,7 +1958,7 @@ class Query(BaseExpression):
warnings.warn(msg, category=RemovedInDjango40Warning)
group_by_cols = annotation.get_group_by_cols()
else:
- if not allow_aliases:
+ if not allow_aliases or alias in column_names:
alias = None
group_by_cols = annotation.get_group_by_cols(alias=alias)
group_by.extend(group_by_cols)