Duplicate key value violates unique constraint "[collection]_pkey"

Hello,

Really enjoying working with Strapi but am currently facing an issue where we’re unable to add a new database record because of the following error:

Duplicate key value violates unique constraint "[collection]_pkey"

This seems to be happening with several tables.

I’m not sure if it’s related to our manual import using a database tool ie… DataGrip.

The only way to resolve it seems to delete all of the records of a table.

We’re using PostgreSQL.

Thanks for your help,

Michael

Hey,

I figured a workaround that worked for me. I transfered all the entries from the sequence table from my initial setup. The sequence table is used to hold the primary keys of the schema. If you don’t have the same sequence table the primary keys will be generated from the start and you will end up having duplicate primary keys and that why the error occurs.

1 Like

Thanks for your response here.

The error here really is just related to the next time we try to add a record to the database. Perhaps the issue can be resolved by altering the sequence?

https://www.postgresqltutorial.com/postgresql-sequences/

ALTER SEQUENCE serial RESTART WITH 105;

2 Likes

This was so useful! I switch from GCP to Railway and this was a pain in the ass but I use that Query and magic! Thank you! :mage:

@michaelstokes93 Thank you very much!