Skip to content

Instantly share code, notes, and snippets.

@DRBragg
Last active June 18, 2020 16:17
Show Gist options
  • Save DRBragg/a1fc1e7d7cb796cd4191433619eff34d to your computer and use it in GitHub Desktop.
Save DRBragg/a1fc1e7d7cb796cd4191433619eff34d to your computer and use it in GitHub Desktop.
To reset ActiveRecords id sequence after restoring DB from a dump
# 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