Skip to content

Fix invalid SQL on DELETE with backward relations/joins#2217

Open
sumit-raghav-06 wants to merge 2 commits into
tortoise:developfrom
sumit-raghav-06:fix/delete-backward-relation-join
Open

Fix invalid SQL on DELETE with backward relations/joins#2217
sumit-raghav-06 wants to merge 2 commits into
tortoise:developfrom
sumit-raghav-06:fix/delete-backward-relation-join

Conversation

@sumit-raghav-06

Copy link
Copy Markdown

Description

Refactored the DeleteQuery._make_query method to handle deletions when filtering across relations or implicit filters that require a table JOIN.

Modern SQL backends like PostgreSQL and SQLite do not natively support standard DELETE FROM table LEFT JOIN ... syntax. This fix detects if joins are present (self._joined_tables) and refactors the statement into a clean, isolated IN subquery fallback.

Motivation and Context

This fixes an issue where running a .delete() on queries involving backward relations or joined filters throws an operational/syntax error on PostgreSQL and SQLite

How Has This Been Tested?

  • Verified that simple deletes continue to use the optimized, direct standard execution path.
  • Verified that deletes involving table joins successfully refactor into the WHERE id IN (SELECT ...) subquery format and execute without syntax errors

Checklist:

  • My code follows the code style of this project.
  • I have added the changelog accordingly.

@codspeed-hq

codspeed-hq Bot commented Jun 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing sumit-raghav-06:fix/delete-backward-relation-join (8d53a3b) with develop (ff7ef94)

Open in CodSpeed

@sumit-raghav-06 sumit-raghav-06 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guide me if anything wrong

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.

1 participant