nopSolutions / nopCommerce

ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
https://www.nopcommerce.com
Other
9.29k stars 5.32k forks source link

BulkCopy with KeepIdentity doesn't update the identity sequence in PostgreSQL #7393

Open exileDev opened 1 week ago

exileDev commented 1 week ago

nopCommerce version: develop

The problem is in first insertion, it will call BulkInsertEntitiesAsync:

/// <returns>A task that represents the asynchronous operation</returns>
protected virtual async Task InsertInstallationDataAsync<T>(params T[] entities) where T : BaseEntity
{
    await _dataProvider.BulkInsertEntitiesAsync(entities);
}

public override async Task BulkInsertEntitiesAsync<TEntity>(IEnumerable<TEntity> entities)
{
using var dataContext = CreateDataConnection(LinqToDbDataProvider);
await dataContext.BulkCopyAsync(new BulkCopyOptions() { KeepIdentity = true }, entities.RetrieveIdentity(dataContext));

}


> 
> and for second insertion, it calls InsertEntityAsync:
> 
> ```c#
> protected virtual async Task<T> InsertInstallationDataAsync<T>(T entity) where T : BaseEntity
> {
>     return await _dataProvider.InsertEntityAsync(entity);
> }
> ```
> 
> There is a minor difference in call these two methods, in BulkInsertEntitiesAsync, it will RetrieveIdentity (so count the largest id from table, and use that for the start id of all bulk insert entities), this will not affect the current value of Product_Picture_Mapping_Id_seq, after inserted two ProductPicture records, the current value for Product_Picture_Mapping_Id_seq is still 0.
> 
> And then it try to insert a ProductPicture with InsertEntityAsync, this time, it will use the current sequence id from Product_Picture_Mapping_Id_seq, so we will get allocated id = 1, but this id already existed in ProductPicture table, so you will get an error:
> 
> duplicate key value violates unique constraint "PK_Product_Picture_Mapping"

Source: https://www.nopcommerce.com/en/boards/topic/101149/is-postgres-database-supported-in-nopcommerce
exileDev commented 1 week ago

Waiting for details from the LinqToDB team https://github.com/linq2db/linq2db/issues/4702