From 6d0cbe42c3d382e5393d4af48185c546bb0ada1f Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 20 Apr 2021 22:25:52 -0400 Subject: Fixed #32650 -- Fixed handling subquery aliasing on queryset combination. This issue started manifesting itself when nesting a combined subquery relying on exclude() since 8593e162c9cb63a6c0b06daf045bc1c21eb4d7c1 but sql.Query.combine never properly handled subqueries outer refs in the first place, see QuerySetBitwiseOperationTests.test_subquery_aliases() (refs #27149). Thanks Raffaele Salmaso for the report. --- django/db/models/sql/query.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'django/db/models/sql/query.py') diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6c728d17bf..be08c55c7c 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -631,6 +631,10 @@ class Query(BaseExpression): joinpromoter.add_votes(rhs_votes) joinpromoter.update_join_types(self) + # Combine subqueries aliases to ensure aliases relabelling properly + # handle subqueries when combining where and select clauses. + self.subq_aliases |= rhs.subq_aliases + # Now relabel a copy of the rhs where-clause and add it to the current # one. w = rhs.where.clone() -- cgit v1.3