diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index a7bc0610c81..07c3fdbd344 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -565,8 +565,7 @@ class Query(BaseExpression): col_alias = f"__col{index}" col_ref = Ref(col_alias, col) col_refs[col] = col_ref - inner_query.annotations[col_alias] = col - inner_query.append_annotation_mask([col_alias]) + inner_query.add_annotation(col, col_alias) replacements[col] = col_ref outer_query.annotations[alias] = aggregate.replace_expressions( replacements