I have an existing database that I migrated from SQLAlchemy to the new PostgreSQL database.
I moved all primary keys with the same values ββas before. Now I have tables filled with data, but the corresponding sequences start with 1. I have pk values ββstored from 1 to 2000.
Now when I try to save something with Django, I have
a duplicate key value violates a unique restriction on the Primary key.
How to change the initial values ββof a sequence or to avoid this situation?
My current solution:
conn = psycopg2.connect(...) for table_name in table_names: cursor = conn.cursor() cursor.execute(""" SELECT setval('%s_id_seq', (SELECT COALESCE(MAX(id),0)+1 FROM %s)); """% (table_name, table_name))
It works for me, but I don't like it.
source share