Skip to content

ExecuteUpdate/Delete produce invalid SQL when table is present both as the primary and in a join #3253

@roji

Description

@roji

Test NonSharedModelBulkUpdatesTestBase.Replace_ColumnExpression_in_column_setter:

public virtual async Task Replace_ColumnExpression_in_column_setter(bool async)
{
    var contextFactory = await InitializeAsync<Context28671>();

    await AssertUpdate(
        async,
        contextFactory.CreateContext,
        ss => ss.Set<Owner>().SelectMany(e => e.OwnedCollections),
        s => s.SetProperty(o => o.Value, "SomeValue"),
        rowsAffectedCount: 0);
}

... causes the following SQL to get generated:

UPDATE "OwnedCollection" AS o0
SET "Value" = 'SomeValue'
FROM "Owner" AS o,
	"OwnedCollection" AS o0
WHERE o."Id" = o0."OwnerId"

... which fails because OwnedCollection is present twice (duplicate alias o0).

The SQL Server SQL generation always includes the primary table in the FROM, and only ever outputs the alias for the primary table:

UPDATE [o0]  
SET [o0].[Value] = N'SomeValue'  
FROM [Owner] AS [o]  
INNER JOIN [OwnedCollection] AS [o0] ON [o].[Id] = [o0].[OwnerId]

Ideally, the SQL wouldn't even reference the Owner table (dotnet/efcore#33946). But in the meantime, on the PG side the fix is most likely to have a step which checks if the primary table is present in a join, and to remove the join if so.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions