Changing the default postgreSQL id sequence in Rails
I have a sql dump file that I imported into my database. All records from this sql dump have id ranging from 900 to 2500.
So the problem is that if I create a new entry from my rails app it gets auto id = 1, the next one gets 2,3,4, etc. I think when I create 900's records, I will get an error as id = 900 already exists.
Note. I cannot change my id to start at 1 in my sql dump file as another table is referencing it by id.
Is there any solution?
I am using PostgreSQL
source to share
Rails gets the id it uses from the database, in the case of postgres there are corresponding sequences for each field in the table id
, so to make Rails skip that block id
, you just need to update the corresponding sequence:
ALTER SEQUENCE plural_table_name_id_seq RESTART WITH 2501;
You can put this in a Rails migration if you like, or just run it on your databases.
source to share