I'm using drizzle-orm to work on my Postgresql database...
I have this simple table:
export const ExperimentData = pgTable('experiment_data', {
id: bigserial('id', { mode: 'number' }).primaryKey(),
timestamp: bigint('timestamp', { mode: 'number' }),
websiteId: bigint('website_id', { mode: 'number' }),
experimentId: bigint('experiment_id', { mode: 'number' }),
variantId: bigint('variant_id', { mode: 'number' }),
event: varchar('event', { mode: 'string', length: 255 }),
data: json('data'),
});
and I have a script that reads data from Redis and writes to this table:
await statisticsdb.insert(ExperimentData).values({ timestamp, websiteId, experimentId, variantId, event, data });
this script runs is supposed to run every hour, triggered by a cronjob and now it doesn't run because I get the error
PostgresError: duplicate key value violates unique constraint "experiment_data_pkey"
code: "23505"
at ErrorResponse (/home/ubuntu/production/pertento/node_modules/postgres/src/connection.js:792:23)
at handle (/home/ubuntu/production/pertento/node_modules/postgres/src/connection.js:811:5)
at data (/home/ubuntu/production/pertento/node_modules/postgres/src/connection.js:562:3)
at emit (native:1:1)
at addChunk (native:1:1)
at readableAddChunk (native:1:1)
at data (native:1:1)
this error is caused on the first insertion try.
id
fieldI don't know exactly what to do here. Also, don't want to mess up the data in Postgres nor in Redis.
After running
select * from public.experiment_data_id_seq;
I get the value: 253020
looking to the database, the latest ID is 1171578
which is waaaay off.
How can I fix this? Can I just update this value in the database directly?
Find who or what changed the sequence and make sure they/it are prevented from doing that again.
Pick a number that is max(id) + 100
or so and do:
BEGIN;
ALTER SEQUENCE public.experiment_data_id_seq RESTART <above_number>;
ROLLBACK
or COMMIT
above depending on success or failure.