Skip to content

Commit 662ea58

Browse files
Copilotrenemadsen
andcommitted
Allow CrossApply/OuterApply in DELETE/UPDATE to reach database or be translated
This fixes the issue where tests expected MySqlException but got InvalidOperationException on older MariaDB versions. Co-authored-by: renemadsen <[email protected]>
1 parent 9a53e76 commit 662ea58

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/EFCore.MySql/Query/ExpressionVisitors/Internal/MySqlCompatibilityExpressionVisitor.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,30 @@ protected virtual Expression VisitRowNumber(RowNumberExpression rowNumberExpress
5858

5959
protected virtual Expression VisitCrossApply(CrossApplyExpression crossApplyExpression)
6060
{
61-
// When inside DELETE/UPDATE operations and DeleteWithSelfReferencingSubquery is not supported,
62-
// allow the query to reach the database so it can throw the expected MySqlException
63-
// (Error Code 1093: "You can't specify target table for update in FROM clause")
64-
var shouldCheckSupport = !_insideDeleteOrUpdate || _options.ServerVersion.Supports.DeleteWithSelfReferencingSubquery;
65-
return CheckSupport(crossApplyExpression, shouldCheckSupport && _options.ServerVersion.Supports.CrossApply);
61+
// When inside DELETE/UPDATE operations, allow the query to pass through without checking support.
62+
// This allows:
63+
// - Older databases to throw MySqlException (Error 1093: "You can't specify target table for update in FROM clause")
64+
// - Newer databases to translate to JOIN LATERAL or handle natively
65+
if (_insideDeleteOrUpdate)
66+
{
67+
return base.VisitExtension(crossApplyExpression);
68+
}
69+
70+
return CheckSupport(crossApplyExpression, _options.ServerVersion.Supports.CrossApply);
6671
}
6772

6873
protected virtual Expression VisitOuterApply(OuterApplyExpression outerApplyExpression)
6974
{
70-
// When inside DELETE/UPDATE operations and DeleteWithSelfReferencingSubquery is not supported,
71-
// allow the query to reach the database so it can throw the expected MySqlException
72-
// (Error Code 1093: "You can't specify target table for update in FROM clause")
73-
var shouldCheckSupport = !_insideDeleteOrUpdate || _options.ServerVersion.Supports.DeleteWithSelfReferencingSubquery;
74-
return CheckSupport(outerApplyExpression, shouldCheckSupport && _options.ServerVersion.Supports.OuterApply);
75+
// When inside DELETE/UPDATE operations, allow the query to pass through without checking support.
76+
// This allows:
77+
// - Older databases to throw MySqlException (Error 1093: "You can't specify target table for update in FROM clause")
78+
// - Newer databases to translate to LEFT JOIN LATERAL or handle natively
79+
if (_insideDeleteOrUpdate)
80+
{
81+
return base.VisitExtension(outerApplyExpression);
82+
}
83+
84+
return CheckSupport(outerApplyExpression, _options.ServerVersion.Supports.OuterApply);
7585
}
7686

7787
protected virtual Expression VisitExcept(ExceptExpression exceptExpression)

0 commit comments

Comments
 (0)