Skip to content

Delete based on a join? #2785

Answered by simolus3
Foxsterc asked this question in Q&A
Discussion options

You must be logged in to vote

It's not possible to have joins on delete statements, but in most cases it's possible to come up with a subquery to use in WHERE that does the same thing. E.g. if you wanted to do DELETE FROM t1 JOIN t2 ON t2.foo = t1.bar WHERE t2.another = 3;, you could instead do

final deleteStmt = delete(t1);
deleteStmt.where((t1) {
  final subquery = db.select(t2)
    ..where((t2) => t2.foo.equalsExp(t1.bar) & t2.another.equals(3));
  return existsQuery(subquery);
});

await deleteStmt.go();

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Foxsterc
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants