Entity Framework Core PostgreSQL Inserting -2147482647 Id - sql

I have a .NET Core 2.0 project that is using a Postgres Database which I am using EntityFrameworkCore.PostgreSQL (2.0.1) which is inserting a strange value for my table Id.
This seems to happen randomly and seems to ignore my ID sequence completely.
It inserts the smallest int32 value -2147482647 as the ID a row that is inserted. Then all future inserts cannot happen and it present this error:
The instance of entity type 'Object' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Before Entity saves I know it uses a garbage ID which I assume is this negative, but from my understanding when you Save that ID should be updated to match your sequence. And it looks like future inserts are also getting the same garbage ID which is causing the error I am receiving.
Wondering if anyone else has had this issue, and if so what is the cause.

Late answer but for others finding this question: Yes, I've seen this, and in the combination EF Core + PostgreSQL database too. Please ensure that your primary column's sequence value is correct! For us, the "current value" it was somehow incorrectly set so that the next one would hit an already existing id.
What I think is going on here is that this is treated like a special value for new rows but it's exposed to the user when a new row can't be inserted for database reasons. I don't think this is normally Entity Framework Core issues.

Related

SQLite - any long-term downsides to using unique, non-PK columns as FKs?

In my design, I have many tables which use FKs. The issue is because certain records will be deleted and re-added at various points of time as they are linked to specific project files, the references will be always be inaccurate if I rely on the traditional auto-incrementing ID (because each time they are re-added they will be given a new ID).
I previously asked a question (Sqlite - composite PK with two auto-incrementing values) as to whether I can create a composite auto-incrementing ID however it appears to not be possible as answered by the question I was linked.
The only automatic value I can think of that'll always be unique and never repeated is a full date value, down to the second - however the idea of using a date for the tables' IDs feels like bad design. So, if I instead place a full date field in every table and use these as the FK reference, am I looking at any potential issues down the line? And am I correct in thinking it would be more efficient to store it as integer rather than a text value?
Thanks for the help
Update
To clarify, I am not looking asking in regards to Primary Keys. The PK will be standard auto-incrementing ID. I am asking in regards to basing hundreds of FKs on dates.
Thank you for the replies below, the difficulty i'm having is I can't find a similar model to learn from. The end result is i'd like the application to use project files (like Word has their docx files) to import data into the database. Once a new project is loaded, the previous project's records are cleared but their data is preserved in the project file (the application's custom file format / a txt file) so they can be added once again. The FKs will all be project-based so they will only be referencing records that exist at the time in the database. For example, as it's a world-building application, let's say a user adds a subject type that would be relevant to any project (e.g. mathematics), due to the form it's entered on in the application, the record is given a_type number of 1, meaning it’s something that persists regardless of the project loaded. Another subject type however may be Demonology which only applies to the specific project loaded (e.g. a fantasy world). A school_subject junction table needs both of these in the same table to reference as the FK. So let’s say Demonology is the second record in the subjects type table, it has an auto-increment value of 2 - thus the junction table records 2 as it’s FK value. The issue is, before this project is re-opened again, the user may have added 10 more subject types that are universal and persist, thus next time the project’s subject type records and school_subject records are added back, Demonology is now given the ID of 11. However, the school_subject junction table is re-recreated with the same record having 2 as its value. This is why I’d like a FK which will always remain the same. I don’t want all projects to be present in the database, because I want users to be able to backup and duplicate individual projects as well know that even if the application is deleted, they can re-download and re-open their project files.
This is a bit long for a comment.
Something seems wrong with your design. When you delete a row in a table, there should be no foreign key references to that key. The entity is gone. Does not exist (as far as the database is concerned). Under most circumstances, you will get an error if you try to delete a row in one table where another row refers to that row using a foreign key reference.
When you insert a row into a table, the database becomes aware of that entity. There should not be references to it.
Hence, you have an unusual situation. It sounds like you have primary keys that represent something in the real world -- such as a social security number or vehicle identification number. If that is the case, you might want this id to be the primary key of the table.
Another option is soft deletion. Once one of these rows is inserted in the table, it cannot be deleted. However, you can set a flag that says that it is deleted. Then, foreign key references can stay to the "soft" deleted row.

MVC 5 Database first invalid column name

I am getting an Invalid column name error on column "c1" even though "c1" is clearly in the relevant table of the relevant database, and spelled exactly the same.
I even re-did another project with the New Model command, which uses ADO.Net Entity Data Model, which inturn uses Code First From the database (and possibly behind the scenes uses EF, my version of EF is 6.0.0.0) and the same column name came through as in the week old project I started with.
This column is a foreign key (integer), and perhaps that has something to do with it.
Specifically, the version of MVC I am using is 5.2.2.0
This version of MVC tries to "include" the table that the foreign key references and that table's model is one of the members in the model with the error. This may have something to do with the problem.
Does anyone please have any insight into what is going on? Thanks....

How to implement using NHibernate UInt64 identifier generator with the option to set its initialize value

Is it possible to use NHibernate for getting unique identifier of type: UInt64.
The initialize value of this property must be unique (for more than 1 DB).
The usage: I need to get this value and increment it by 1.
This action should be a closed transaction.
usually we want to have unique keys in one DB, not for multiple databases, GUIDs are used there as far as I know. Getting the max(int64) out of both DBs and then picking the largests and adding 1 will work but that sounds rather clumsy I think? What do you need this for actually?
Hope this helps,
What about using database identities (or similar) and setting the starting value for database N as N, and the increment by value (how much you add to the current latest key every time you get a new one) equal to the number of databases?
That way each database should keep a distinct set of ids. Only problem would be in case you need to add a new database to the set.

How does rails come up with the ID for a new model/record?

How does activerecord assign an ID to a newly created record? The ID values seem to be all over the place. Sometimes they are sequential, but sometimes they seem to be some kind of a hash.
Is there a way to control the behavior?
Within a relational database you'll see that IDs are usually sequential. This happens to be an automatically incrementing field called id by default in these databases with Rails. This is the 99% case, meaning that 99% of the time you can expect to see it done this way. It's the sane way.
However, There are some cases in which the "id" field within the database may not be automatically incrementing and may instead be a string. In a database I am working with at the moment, the id field is called client_id, is a 6-character string such as "RAB001" and needs to be manually assigned by the code itself. This is due to a legacy system we are supporting and there's nothing we can do to fix that. It's just how it is.
In other databases such as Mongoid the ids are, once again, generated automatically. There's a difference here though: instead of them being automatically incrementing numbers they are a hash. In a Mongo database I happen to have handy, one of the object's _id fields (note the underscore) is this lovely, easy-to-understand1 hash: 4e22b5812f8b7d6f6d000001. This is automatically generated by Mongo and I don't really care what it is except for when I need to find an object and there's no other way of finding it by another unique value.
I would recommend sticking with an automatically generating ID system, be it something provided by the traditional database systems such as PostgreSQL or MySQL or something by Mongo.
Any system where you need to generate the primary key for a record manually needs to have a huge "HERE BE DRAGONS" label on it and should be handled like a case of nitroglycerin or similarly to this apt analogy. Avoid this system if you can.
1 I am being sarcastic here.

Hibernate and IDs

Is it possible in hibernate to have an entity where some IDs are assigned and some are generated?
For instance:
Some objects have an ID between 1-10000 that are generated outside of the database; while some entities come in with no ID and need an ID generated by the database.
You could use 'assigned' as the Id generation strategy, but you would have to give the entity its id before you saved it to the database. Alternately you could build your own implementation of org.hibernate.id.IdentifierGenerator to provide the Id in the manner you've suggested.
I have to agree w/ Cade Roux though, and doing so seems like it be much more difficult than using built in increment, uuid, or other form of id generation.
I would avoid this and simply have an auxiliary column for the information about the source of the object and a column for the external identifier (assuming the external identifier was an important value you wanted to keep track of).
It's generally a bad idea to use columns for mixed purposes - in this case to infer from the nature of a surrogate key the source of an object.
Use any generator you like, make sure it can start at an offset (when you use a sequence, you can initialize it accordingly).
For all other entities, call setId() before you insert them. Hibernate will only generate an id if the id property is 0. Note that you should first insert objects with ids into the db and then work with them. There is a lot of code in Hibernate which expects the object to be in the DB when id != 0.
Another solution is to use negative ids for entities which come with an id. This will also make sure that there are no collisions when you insert an new object.