Last active
June 18, 2020 16:17
-
-
Save DRBragg/a1fc1e7d7cb796cd4191433619eff34d to your computer and use it in GitHub Desktop.
To reset ActiveRecords id sequence after restoring DB from a dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TABLE_NAME = the name of the table you need to reset the id sequence for | |
ActiveRecord::Base.connection.execute(%q{ | |
select setval('TABLE_NAME_id_seq', m) | |
from ( | |
select max(id) from TABLE_NAME | |
) as dt(m) | |
}) | |
# OR | |
ActiveRecord::Base.connection.tables.each do |t| | |
ActiveRecord::Base.connection.reset_pk_sequence!(t) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment