summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2022-09-28 10:51:06 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-10-06 12:04:00 +0200
commitc6350d594c359151ee17b0c4f354bb44f28ff69e (patch)
treee01d3ff1213d480eb07e5bd365234576613912ce /django/db/models/sql/query.py
parentb7b28c7c189615543218e81319473888bc46d831 (diff)
downloaddjango-c6350d594c359151ee17b0c4f354bb44f28ff69e.tar.gz
Refs #30158 -- Removed alias argument for Expression.get_group_by_cols().
Recent refactors allowed GROUP BY aliasing allowed for aliasing to be entirely handled by the sql.Query.set_group_by and compiler layers.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 9d4ca0ffb6..c2a71ff589 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1147,13 +1147,11 @@ class Query(BaseExpression):
if col.alias in self.external_aliases
]
- def get_group_by_cols(self, alias=None, wrapper=None):
+ def get_group_by_cols(self, wrapper=None):
# If wrapper is referenced by an alias for an explicit GROUP BY through
# values() a reference to this expression and not the self must be
# returned to ensure external column references are not grouped against
# as well.
- if alias:
- return [Ref(alias, wrapper or self)]
external_cols = self.get_external_cols()
if any(col.possibly_multivalued for col in external_cols):
return [wrapper or self]
@@ -2247,11 +2245,16 @@ class Query(BaseExpression):
self.select = tuple(values_select.values())
self.values_select = tuple(values_select)
group_by = list(self.select)
- if self.annotation_select:
- for alias, annotation in self.annotation_select.items():
- if not allow_aliases or alias in column_names:
- alias = None
- group_by_cols = annotation.get_group_by_cols(alias=alias)
+ for alias, annotation in self.annotation_select.items():
+ if not (group_by_cols := annotation.get_group_by_cols()):
+ continue
+ if (
+ allow_aliases
+ and alias not in column_names
+ and not annotation.contains_aggregate
+ ):
+ group_by.append(Ref(alias, annotation))
+ else:
group_by.extend(group_by_cols)
self.group_by = tuple(group_by)