diff --git a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java index 734fa96f72..31ad1c3d66 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MergeImpl.java @@ -1207,8 +1207,11 @@ implements @Override public final MergeImpl deleteWhere(Condition condition) { + // The ordering is to run deletions *before* updates in order to prevent + // constraint violations that may occur from updates, otherwise. + // See https://github.com/jOOQ/jOOQ/issues/7291#issuecomment-610833303 if (matchedClause) - matched.add(new MatchedClause(condition, true)); + matched.add(matched.size() - 1, new MatchedClause(condition, true)); else throw new IllegalStateException("Cannot call where() on the current state of the MERGE statement");