|
@@ -1927,6 +1927,19 @@ class Query(BaseExpression):
|
|
|
primary key, and the query would be equivalent, the optimization
|
|
|
will be made automatically.
|
|
|
"""
|
|
|
+
|
|
|
+ if allow_aliases:
|
|
|
+ column_names = set()
|
|
|
+ seen_models = set()
|
|
|
+ for join in list(self.alias_map.values())[1:]:
|
|
|
+ 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():
|
|
@@ -1940,7 +1953,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)
|