Skip to content

Conversation

Copy link

Copilot AI commented Dec 16, 2025

MySQL doesn't support DELETE statements with self-referencing subqueries that reference the deleted table (error 1093: "can't specify target table for update in FROM clause"). MariaDB < 11.0 has the same limitation.

The Delete_with_cross_apply, Delete_with_outer_apply, Delete_Where_optional_navigation_predicate, and Delete_with_RightJoin tests were failing because they generate DELETE statements with correlated subqueries that reference the table being deleted.

Examples include:

LATERAL joins:

DELETE `o`
FROM `Order Details` AS `o`
JOIN LATERAL (
    SELECT 1
    FROM `Orders` AS `o0`
    WHERE `o0`.`OrderID` < `o`.`OrderID`  -- References the deleted table
    ...
) AS `o1` ON TRUE

EXISTS subqueries:

DELETE `o`
FROM `Order Details` AS `o`
WHERE EXISTS (
    SELECT 1
    FROM `Order Details` AS `o0`  -- Same table being deleted
    ...
    WHERE ... AND (`o0`.`OrderID` = `o`.`OrderID`)  -- References the deleted table
)

Changes

  • Added conditional logic to four test methods checking AppConfig.ServerVersion.Supports.DeleteWithSelfReferencingSubquery
  • When supported (MariaDB >= 11.0): Tests run normally with SQL assertion
  • When not supported (MySQL all versions, MariaDB < 11.0): Tests expect MySqlException

This follows the established pattern used in Delete_predicate_based_on_optional_navigation and Delete_entity_with_auto_include tests.

Original prompt

This section details on the original issue you should resolve

<issue_title>MySqlConnector.MySqlException : You can't specify target table 'o' for update in FROM clause</issue_title>
<issue_description>```
Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_with_cross_apply(async: False) [259 ms]
Error Message:
MySqlConnector.MySqlException : You can't specify target table 'o' for update in FROM clause
Stack Trace:
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in //src/MySqlConnector/Core/ServerSession.cs:line 1125
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /
/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 133
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 489 at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 58 at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 304 at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.<>c.<NonQueryResult>b__30_0(DbContext _, ValueTuple3 state)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.NonQueryResult(RelationalQueryContext relationalQueryContext, RelationalCommandResolver relationalCommandResolver, Type contextType, CommandSource commandSource, Boolean threadSafetyChecksEnabled)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteDelete[TSource](IQueryable`1 source)
at Microsoft.EntityFrameworkCore.TestUtilities.BulkUpdatesAsserter.<>c__DisplayClass6_0`1.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.<>c__DisplayClass41_0`1.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__7`1.<b__7_0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_0`2.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.ExecuteWithStrategyInTransactionAsync[TContext](Func`1 createContext, Action`2 useTransaction, Func`2 testOperation, Func`2 nestedTestOperation1, Func`2 nestedTestOperation2, Func`2 nestedTestOperation3)
at Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_with_cross_apply(Boolean async) in /home/runner/work/Pomelo.EntityFrameworkCore.MySql/Pomelo.EntityFrameworkCore.MySql/test/EFCore.MySql.FunctionalTests/BulkUpdates/NorthwindBulkUpdatesMySqlTest.cs:line 880
--- End of stack trace from previous location ---


## Comments on the Iss...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes microting/Pomelo.EntityFrameworkCore.MySql#222

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Copilot AI changed the title [WIP] Fix MySqlConnector exception for update in FROM clause Fix DELETE with LATERAL JOIN tests for MySQL error 1093 Dec 16, 2025
Copilot AI requested a review from renemadsen December 16, 2025 13:20
@renemadsen
Copy link
Member

@copilot We have a similar issue here:

Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_Where_optional_navigation_predicate(async: False) [534 ms]
Error Message:
MySqlConnector.MySqlException : You can't specify target table 'o' for update in FROM clause
Stack Trace:
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in //src/MySqlConnector/Core/ServerSession.cs:line 1125
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /
/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 133
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 489 at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 58 at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 304 at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.<>c.<NonQueryResult>b__30_0(DbContext _, ValueTuple3 state)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.NonQueryResult(RelationalQueryContext relationalQueryContext, RelationalCommandResolver relationalCommandResolver, Type contextType, CommandSource commandSource, Boolean threadSafetyChecksEnabled)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteDelete[TSource](IQueryable1 source) at Microsoft.EntityFrameworkCore.TestUtilities.BulkUpdatesAsserter.<>c__DisplayClass6_01.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.<>c__DisplayClass41_01.<<ExecuteWithStrategyInTransactionAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__71.<b__7_0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_02.<<ExecuteAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.ExecuteWithStrategyInTransactionAsync[TContext](Func1 createContext, Action2 useTransaction, Func2 testOperation, Func2 nestedTestOperation1, Func2 nestedTestOperation2, Func`2 nestedTestOperation3)
at Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_Where_optional_navigation_predicate(Boolean async) in /home/runner/work/Pomelo.EntityFrameworkCore.MySql/Pomelo.EntityFrameworkCore.MySql/test/EFCore.MySql.FunctionalTests/BulkUpdates/NorthwindBulkUpdatesMySqlTest.cs:line 738
--- End of stack trace from previous location ---

Copy link
Author

Copilot AI commented Dec 16, 2025

@copilot We have a similar issue here:

Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_Where_optional_navigation_predicate(async: False) [53...

Fixed in commit b9ac95d. Applied the same conditional logic to Delete_Where_optional_navigation_predicate - the test now checks AppConfig.ServerVersion.Supports.DeleteWithSelfReferencingSubquery and expects MySqlException when not supported (MySQL all versions, MariaDB < 11.0).

Copilot AI changed the title Fix DELETE with LATERAL JOIN tests for MySQL error 1093 Fix DELETE with self-referencing subquery tests for MySQL error 1093 Dec 16, 2025
@renemadsen
Copy link
Member

@copilot Run all the tests in Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest and find the failing ones, since it follows the same pattern

Copy link
Author

Copilot AI commented Dec 16, 2025

@copilot Run all the tests in Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest and find the failing ones, since it follows the same pattern

I analyzed all Delete tests in NorthwindBulkUpdatesMySqlTest. Most tests with self-referencing DELETE patterns already have the conditional logic in place. Here's the summary:

Tests with conditional logic (already fixed):

  • Delete_Where_OrderBy through Delete_Where_Skip_Take (7 tests)
  • Delete_GroupBy_Where_Select, Delete_GroupBy_Where_Select_2
  • Delete_Where_Skip_Take_Skip_Take_causing_subquery
  • Delete_with_LeftJoin, Delete_with_cross_join
  • Delete_with_cross_apply, Delete_with_outer_apply (fixed in commits 642f944, b9ac95d)
  • Delete_Where_optional_navigation_predicate (fixed in b9ac95d)
  • Delete_with_LeftJoin_via_flattened_GroupJoin

Tests without conditional logic but likely don't need it:

  • Delete_Where, Delete_Where_parameter, Delete_Where_TagWith: Simple WHERE clauses, no self-referencing
  • Delete_with_join: Joins Orders table (different from Order Details being deleted), not self-referencing
  • Delete_with_RightJoin: No SQL validation yet, needs investigation if implemented

The pattern is now well-established: tests that generate DELETE statements with self-referencing subqueries (where the subquery references the same table being deleted) need the AppConfig.ServerVersion.Supports.DeleteWithSelfReferencingSubquery conditional check.

@renemadsen
Copy link
Member

@copilot Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_with_RightJoin(async: True) [596 ms]
Error Message:
MySqlConnector.MySqlException : You can't specify target table 'o' for update in FROM clause
Stack Trace:
at MySqlConnector.Core.ServerSession.ReceiveReplyAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in //src/MySqlConnector/Core/ServerSession.cs:line 1125
at MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in /
/src/MySqlConnector/Core/ResultSet.cs:line 37
at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 133
at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 489 at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 58 at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 304 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.TestUtilities.BulkUpdatesAsserter.<>c__DisplayClass6_01.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.<>c__DisplayClass41_01.<<ExecuteWithStrategyInTransactionAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__71.<b__7_0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_02.<<ExecuteAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func4 operation, Func4 verifySucceeded, TState state, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.TestUtilities.TestHelpers.ExecuteWithStrategyInTransactionAsync[TContext](Func1 createContext, Action2 useTransaction, Func2 testOperation, Func2 nestedTestOperation1, Func2 nestedTestOperation2, Func`2 nestedTestOperation3)
at Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_with_RightJoin(Boolean async) in /home/runner/work/Pomelo.EntityFrameworkCore.MySql/Pomelo.EntityFrameworkCore.MySql/test/EFCore.MySql.FunctionalTests/BulkUpdates/NorthwindBulkUpdatesMySqlTest.cs:line 1883
--- End of stack trace from previous location ---

Copy link
Author

Copilot AI commented Dec 16, 2025

@copilot Failed Pomelo.EntityFrameworkCore.MySql.FunctionalTests.BulkUpdates.NorthwindBulkUpdatesMySqlTest.Delete_with_RightJoin(async: True) [596 ms]
Error Message:
MySqlConnector.MySqlException...

Fixed in commit 4e51b6a. Applied the same conditional logic to Delete_with_RightJoin - the test now checks AppConfig.ServerVersion.Supports.DeleteWithSelfReferencingSubquery and expects MySqlException when not supported (MySQL all versions, MariaDB < 11.0).

@renemadsen renemadsen marked this pull request as ready for review December 16, 2025 14:54
@renemadsen renemadsen merged commit 58866bf into master Dec 16, 2025
24 of 54 checks passed
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.

2 participants