Opened 5 months ago

Closed 5 months ago

#35614 closed Bug (fixed)

SQLCompiler.as_subquery_condition shouldn't modify the query object

Reported by: Csirmaz Bendegúz Owned by: Csirmaz Bendegúz
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Csirmaz Bendegúz Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Csirmaz Bendegúz)

SQLCompiler.as_subquery_condition modifies the query object.

django/db/models/sql/compiler.py#L1619

self.query.where.add(RawSQL("%s = %s" % (lhs_sql, rhs), lhs_params), AND)

This is unfortunate, because whenever the query is re-compiled, it adds another condition.
I noticed the issue when inspecting .query on a queryset. Each time I accessed .query, a new condition appeared.
An example queryset can be found in test MultiColumnFKTests.test_double_nested_query.

Change History (5)

comment:1 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)
Has patch: set

comment:2 by Simon Charette, 5 months ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak, 5 months ago

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In bdd53848:

Fixed #35614 -- Prevented SQLCompiler.as_subquery_condition() from mutating a query.

Note: See TracTickets for help on using tickets.
Back to Top