Answer the question
In order to leave comments, you need to log in
Where do negative PostgreSQL primary key index values come from?
The following question appeared in terms of working with PostgreSQL through the Entity Framework.
There is the following table:
CREATE TABLE "TankContentStateHistory" (
"TankContentStateHistoryId" integer NOT NULL SET DEFAULT nextval('"TankContentStateHistory_TankContentStateHistoryId_seq"'::regclass),
"AverageTemperature" double precision NOT NULL,
...
"GTVolume" double precision DEFAULT 0.0 NOT NULL);
CREATE SEQUENCE "TankContentStateHistory_TankContentStateHistoryId_seq"
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
2021-09-19 00:45:58 MSK ОШИБКА: повторяющееся значение ключа нарушает ограничение уникальности "PK_TankContentStateHistory"
2021-09-19 00:45:58 MSK ПОДРОБНОСТИ: Ключ "("TankContentStateHistoryId")=(-2147482602)" уже существует.
2021-09-19 00:45:58 MSK ОПЕРАТОР: INSERT INTO "TankContentStateHistory" ("TankContentStateHistoryId", ... "GTVolume")
VALUES ($1, ..)
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> Npgsql.PostgresException: 23505: повторяющееся значение ключа нарушает ограничение уникальности "PK_TankContentStateHistory"
в Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
Answer the question
In order to leave comments, you need to log in
Why not use the built-in type serial instead of TankContentStateHistory_TankContentStateHistoryId_seq
CREATE TABLE "TankContentStateHistory" (
"TankContentStateHistoryId" serial NOT NULL,
"AverageTemperature" double precision NOT NULL,
"GTVolume" double precision DEFAULT 0.0 NOT NULL
);
INSERT INTO "TankContentStateHistory" ("AverageTemperature", "GTVolume") VALUES (10, 20);
SELECT * FROM "TankContentStateHistory";
Negative values are a sign that your counter "went to the second round".
Change to BigInt.
In general, this fact and gaps in the continuous numbering of the index indicate, perhaps, a problematic algorithm that intensively uses this table in "temporary" calculations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question