Skip to content

Fix ExecuteDelete with optional navigation generating invalid MySQL (error 1093) - #437

Draft
renemadsen with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-optional-navigation-delete-error
Draft

renemadsen with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-optional-navigation-delete-error

Conversation

Copilot AI commented Aug 27, 2026

Copy link
Copy Markdown

ExecuteDelete filtered through an optional reference navigation produces a LEFT JOIN, which IsValidSelectExpressionForExecuteDelete rejected (only InnerJoinExpression was accepted). EF Core then falls back to subquery pushdown — DELETE FROM t WHERE Id IN (SELECT Id FROM t ...) — which MySQL rejects with error 1093.

MySQL's multi-table DELETE syntax supports LEFT JOIN, so the fix is to accept LeftJoinExpression alongside InnerJoinExpression.

Changes

  • MySqlQueryableMethodTranslatingExpressionVisitor.cs: Widen the join-type check from t is InnerJoinExpression to t is InnerJoinExpression or LeftJoinExpression

Repro

public class Child
{
    public int Id { get; set; }
    public int? ParentId { get; set; }   // optional FK
    public Parent? Parent { get; set; }
}

// Generates invalid subquery DELETE on MySQL 8
await ctx.Children.Where(c => c.Parent!.Id == parentId).ExecuteDeleteAsync();

Before (invalid — error 1093):

DELETE `c` FROM `Children` AS `c`
WHERE `c`.`Id` IN (
    SELECT `c0`.`Id` FROM `Children` AS `c0`
    LEFT JOIN `Parents` AS `p` ON `c0`.`ParentId` = `p`.`Id`
    WHERE `p`.`Id` = @parent_Id)

After (valid multi-table DELETE):

DELETE `c` FROM `Children` AS `c`
LEFT JOIN `Parents` AS `p` ON `c`.`ParentId` = `p`.`Id`
WHERE `p`.`Id` = @parent_Id

… 1093

When ExecuteDelete is filtered through an optional reference navigation,
EF Core produces a LEFT JOIN. The previous check only accepted
InnerJoinExpression, causing EF Core to fall back to subquery pushdown
which MySQL rejects with error 1093 ("You can't specify target table for
update in FROM clause"). MySQL's multi-table DELETE syntax supports LEFT
JOIN, so we now accept both InnerJoinExpression and LeftJoinExpression.

Closes #412

Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ExecuteDelete error 1093 with optional reference navigation Fix ExecuteDelete with optional navigation generating invalid MySQL (error 1093) Aug 27, 2026
Copilot AI requested a review from renemadsen August 27, 2026 08:06
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.

ExecuteDelete through an optional reference navigation generates invalid MySQL (error 1093)

2 participants