OK I am not sure if this is a postgresql-specific problem or not, but I believe it is. If you did something like this (such as in seed.rb):
#!ruby User.create(id: 1, ...)
Then the next time you create new user, the ActiveRecord::RecordNotUnique error would be raised.
But why you specified
I have to admit that this is a lazy and silly decide. I re-created ChordsPresent and migrated from Heroku bamboo stack to cedar stack. The models have some schema-level changes also. Thus the easier way for me to migrate the old data to the new database is:
- download the remote database
- create a rake file to output a seed.rb file which based on the existing data (collecting only the required data, and matching the new schema)
- add the outputted seed.rb file to the new app source code, and push and run it in the cedar stack (so the new database now has all the new data with correct schema)
- revert the seed.rb and destroy the last commit completely (git tracking sensitive data is always a bad practice!, although the most sensitive data are the users' provider and uid only)
- push to heroku again with
-fflag to force replace it
As I want to retain the chords and users id, thus I included the id field of both table. However explicitly assigning the
id field causes problem.
How to fix?
(Provided this is a postgresql problem)
#!ruby ActiveRecord::Base.connection.execute("SELECT setval('table_name_id_seq', (SELECT max(id) FROM table_name));")
table_name with your actual table name. In my case, it is:
#!ruby ActiveRecord::Base.connection.execute("SELECT setval('users_id_seq', (SELECT max(id) FROM users));") ActiveRecord::Base.connection.execute("SELECT setval('chords_id_seq', (SELECT max(id) FROM chords));")