Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulkInsert with Identity and PK causes 'duplicate key value' error #1683

Open
JPL-1 opened this issue Feb 19, 2025 · 1 comment
Open

BulkInsert with Identity and PK causes 'duplicate key value' error #1683

JPL-1 opened this issue Feb 19, 2025 · 1 comment

Comments

@JPL-1
Copy link

JPL-1 commented Feb 19, 2025

I am building an app that uses identity columns. I am finding that the identity columns work fine by themselves, and they work fine with a unique index on the identity column. However, if a PK is added (a unique constraint of type Primary Key), then BulkInsert produces the following error.

Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "Test_pKey"

DETAIL: Key (id)=(0) already exists.
at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Npgsql.NpgsqlBinaryImporter.Complete(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlBinaryImporter.Complete()
2
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.InsertAsync[T](DbContext context, IEnumerable1 entities, TableInfo tableInfo, Action1 progress, Boolean isAsync, CancellationToken cancellationToken)
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.Insert[T](DbContext context, Type type, IEnumerable1 entities, TableInfo tableInfo, Action1 progress)
at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, Type type, IEnumerable1 entities, TableInfo tableInfo, Action1 progress)
at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable1 entities, OperationType operationType, BulkConfig bulkConfig, Action1 progress)
at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IEnumerable1 entities, BulkConfig bulkConfig, Action1 progress, Type type)
at LookingGlass.System.lgImport.TestIdentityInsert() in /Volumes/LaCie-Data/LookingGlass/Code/LookingGlass.System/lgImport.cs:line 554
Exception data:
Severity: ERROR
SqlState: 23505
MessageText: duplicate key value violates unique constraint "Test_pKey"
Detail: Key (id)=(0) already exists.
Where: COPY Test, line 2
SchemaName: Finance
TableName: Test
ConstraintName: Test_pKey
File: nbtinsert.c
Line: 666
Routine: _bt_check_unique

For testing purposes, here is the definition of the 'Test' table.

DROP TABLE IF EXISTS "Finance"."Test";

CREATE TABLE IF NOT EXISTS "Finance"."Test"
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
val text COLLATE pg_catalog."default",
CONSTRAINT "Test_pKey" PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS "Finance"."Test"
OWNER to jloper;

COMMENT ON TABLE "Finance"."Test"
IS 'Testing Bulk Insert';

For testing purposes, here is the code that reproduces this issue. We never get to the success line. Always hits the catch/exception.

public void TestIdentityInsert()
{

    using var context = new TestContext();
    try
    {
        var itmLst = new List<Test>();
        itmLst.Add(new Test() { val = "Foo1" });
        itmLst.Add(new Test() { val = "Bar2" });
        context.BulkInsert(itmLst);
        Console.WriteLine("2 rows were inserted");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}
@JPL-1
Copy link
Author

JPL-1 commented Feb 19, 2025

I have found a work around.

        context.BulkInsert(itmLst, cfg => cfg.PropertiesToExclude = new List<string> { nameof(Test.id) });

If I specifically tell the library to Exclude the id column, then it works.

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

No branches or pull requests

1 participant