-
Notifications
You must be signed in to change notification settings - Fork 237
HasData seeding causes Add to fail with PostgresException: 23505 on calling SaveChanges #759
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
Comments
Always seed with negative values to make sure seeded values don't collide with values later generate from the identity sequence. |
Duplicate of #367 |
"Always seed with negative values to make sure seeded values don't collide with values later generate from the identity sequence." All due respect, but this is not a solution, but an ugly hack. This issue should not be closed, instead, it should be properly addressed and fixed. |
@BojidarStanchev this difficulty is related to the way PostgreSQL manages sequences and serial columns. See conversation with possible proposals in #573 (comment). At the end of the day I'm not sure why negative values is an ugly hack or problematic - why is an id of -1 worse than 1? |
-1 is more negative, we need more positivity in the world. If you consider how many people wasted a few hours researching why this happens there are probably thousands of work hours lost because of this not working as intended. |
The point is that you have to manually write an ID in the migrations, in any case - it just can't be positive. This is how EF Core works, and if you don't you actually get an exception telling you to assign a negative value (so it's better to raise this question with EF Core rather than Npgsql).
Just to make sure there's no confusion, the seed data isn't supposed to be written in the migrations, it's supposed to be written in your context model, via |
Don't use HasData: dotnet/EntityFramework.Docs#1055 |
Uh oh!
There was an error while loading. Please reload this page.
Npgsql.EntityFrameworkCore.PostgreSQL version: 2.2.0-preview1
HasData
seeding causesAdd
to fail when callingSaveChangesAsync
.Example:
Now when you want to add new
Item
, this code won't work and will causePostgresException: 23505
:Note that this code will work fine if we won't use
HasData
.What you have to do instead, you have to set Id explicitly to 0:
Or it starts to work too after inserting the item manually using some DB manager.
I think #367 and #573 is related?
The text was updated successfully, but these errors were encountered: