I exported a table worth of data from one postgres instance as CSV and then directly imported into another postgres instance bypassing rails using the \copy
command like so:
\copy my_table to ‘my_table.csv’ csv;
and
\copy my_table from ‘my_table.csv’ with (format csv);
Then I noticed that Rails was unable to create new records for that table:
MyModel.create(name: “Foo”)
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “my_models_pkey"
I could see quite clearly it was trying to insert with an id
of “1”. The solution is to reset the primary key sequence like this:
ActiveRecord::Base.connection.reset_pk_sequence!(‘my_table’)