EFCore.BulkExtensions icon indicating copy to clipboard operation
EFCore.BulkExtensions copied to clipboard

SQLite - ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint

Open Arslan007 opened this issue 3 years ago • 7 comments

I am using 3.2.5 in UWP app with SQLite. I cannot upgrade to latest version due to .NET Standard 2.1 is not fully supported to UWP so we cannot upgrade to EFCore 5.

I am getting this error on InsertOrUpdate statement.

SQLite Error 1: 'ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint'.

using (var localContext = new TechnicianContext(identityService))
{
    var bulkConfig = new BulkConfig { BatchSize = 5000, SetOutputIdentity = true };
    await localContext.BulkInsertOrUpdateAsync(items.ToList(), bulkConfig);
}

AFAIK there is no duplicate on items list for PKID and no unique constraint at all so I have no idea why its erroring out for this.

Let me know if you need any further info

Arslan007 avatar Nov 04 '21 22:11 Arslan007

@borisdj have you considered my question. Thanks?

Arslan007 avatar Nov 09 '21 23:11 Arslan007

Took a look but not sure what causes the issue. Can you upgrade and try with 6.0, it's just published. If v6 does not supports UVW, then in source with latest v3, try make a test where your issue would be reproducible.

borisdj avatar Nov 10 '21 09:11 borisdj

@borisdj I cannot upgrade to 6.0 due to UWP limitation as it doesn't support .NET Standard 2.1 or later. I tried with source code and its throwing error on this place

image

Let me know if you need more info

Arslan007 avatar Nov 15 '21 19:11 Arslan007

Still not able to reproduce it. If you are already have reproducible environment try to isolate the issue and if you manage to find a fix you can make a PR.

borisdj avatar Nov 16 '21 14:11 borisdj

@borisdj Is there anyway I can print the SQL Query log. I have enabled Enabled logging for EF Context but somehow all other queries are logged but nothing from this Source code. Just wanted to see what query is being executed since everything is fine till LoadSqliteValues method and throws error after first query execution

Arslan007 avatar Nov 16 '21 16:11 Arslan007

The ON CONFLICT Clause is in SqlQueryBuilderSqlite in method InsertIntoTable which is called from GetSqliteCommand in Adapter. So it's more likely that exception is thrown there at command.Prepare(); or in Merge at command.ExecuteNonQuery. In debug you can check CommandText before execution and even copy it and run it manualy in some Query editor on Db.

AS for EF logging: https://eamonkeane.dev/3-ways-to-view-sql-generated-by-entity-framework-core-5/ Also DbCommandInterceptor could be used as well, there are examples in Tests.

borisdj avatar Nov 17 '21 15:11 borisdj

I can relate this to my issue. Its a bug where sqlite requires primary key for UPSERT. #648

Gopichandar avatar Nov 18 '21 18:11 Gopichandar