Fixing sequence problems in PostgreSQL
21st April 2004
This one’s mainly for my own future reference. The following error message in PostgreSQL:
ERROR: Cannot insert a duplicate key into unique index auth_permissions_pkey
Is caused by a table with an automatically incremented primary key (set up using the SERIAL keywords in the table creation statement) getting out of sync somehow with the sequence that tracks the next available primary key. You can re-sync the sequence with the following query:
SELECT setval('auth_permissions_id_seq',
(SELECT max(id) FROM auth_permissions));
More recent articles
- Qwen2.5-Coder-32B is an LLM that can code well that runs on my Mac - 12th November 2024
- Visualizing local election results with Datasette, Observable and MapLibre GL - 9th November 2024
- Project: VERDAD - tracking misinformation in radio broadcasts using Gemini 1.5 - 7th November 2024