skip to Main Content

I migrated some data from my old db to new one with pk(id). What should I do in order to my new db continue from last id of migrated data. I encounter no problem while migrating but when project is running, there is problem while adding new object to the db. Project is done in django

2

Answers


  1. You should change/alter the id of your table
    you can use this command in Postgresql to set id to some specific id, and it will auto-increment from that value.

    SELECT setval(pg_get_serial_sequence('your_table_name', 'id'), (SELECT MAX(id) FROM your_table_name));
    

    or

    SELECT setval(pg_get_serial_sequence('your_table_name', 'id'), 'specific_value');
    
    Login or Signup to reply.
  2. You can set the sequence of your pk automatically to the last number (if your pk is an auto-increment integer) with the following PostgreSQL command:

    SELECT SETVAL('<schema>.<table_name>_id_seq', COALESCE(MAX(id), 1) ) FROM <schema>.<table_name>;
    

    Change <schema> and <table_name> to the correct values before running the command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search