Skip to content

fix(queryset): use subquery for DELETE/UPDATE filtering by related fields#2139

Open
noy-solvin wants to merge 3 commits intotortoise:developfrom
noy-solvin:fix/queryset
Open

fix(queryset): use subquery for DELETE/UPDATE filtering by related fields#2139
noy-solvin wants to merge 3 commits intotortoise:developfrom
noy-solvin:fix/queryset

Conversation

@noy-solvin
Copy link

Description

Modified DeleteQuery._make_query() and UpdateQuery._make_query() in tortoise/queryset.py to use a subquery pattern - WHERE id IN (SELECT id FROM (SELECT id FROM table JOIN ... WHERE ...) AS _t).
Also preserved LIMIT and ORDER BY clauses within the internal subquery.

Motivation and Context

DELETE and UPDATE queries were failing when filtering by related fields (foreign keys) because the engine was trying to use JOINs, which MySQL and SQLite don't support for these operations.
closes #283

How Has This Been Tested?

Added test_delete_filter_with_foreign_key and test_update_filter_with_foreign_key to the test suite.
Verified that the full regression suite (1899 tests) passes. The fix was also tested manually.

Full transparency: this fix was generated using Solvin, an AI coding agent my team is building. Reviewed and tested manually before submitting. I'd love your feedback. The fix was fully tested manually by me prior to submitting this PR.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 12, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing noy-solvin:fix/queryset (b453065) with develop (5e65d83)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delete with related field query fails due to invalid SQL generation

2 participants