Re: [Django] #35099: Combining QuerySets with "|" or "&" mutates right-hand side. (was: Combining QuerySets with "|" or "&" produce side effects affecting further queries)

8 views
Skip to first unread message

Django

unread,
Jan 10, 2024, 3:40:13 AM1/10/24
to django-...@googlegroups.com
#35099: Combining QuerySets with "|" or "&" mutates right-hand side.
-------------------------------------+-------------------------------------
Reporter: Alan | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted


Comment:

Thanks for the report.This is caused by
[https://github.com/django/django/blob/9b02ad91ead3db75036be981bab2083aebc993a6/django/db/models/sql/query.py#L688
bumping prefixes], we should probably clone `rhs` before doing this, e.g.
{{{#!diff
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index a79d66eb21..5539f35b1c 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -685,6 +685,7 @@ class Query(BaseExpression):
# except if the alias is the base table since it must be present
in the
# query on both sides.
initial_alias = self.get_initial_alias()
+ rhs = rhs.clone()
rhs.bump_prefix(self, exclude={initial_alias})

# Work out how to relabel the rhs aliases, if necessary.
}}}
Does it work for you? Would you like to prepare a patch? (a regression
test is required).

--
Ticket URL: <https://code.djangoproject.com/ticket/35099#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages