Skip to content

Instantly share code, notes, and snippets.

@joseramonc
Last active February 25, 2022 20:02
Show Gist options
  • Save joseramonc/664fe7a7357b5806758581eab9fc45b8 to your computer and use it in GitHub Desktop.
Save joseramonc/664fe7a7357b5806758581eab9fc45b8 to your computer and use it in GitHub Desktop.
Script to reset all tables primary keys for a rails app
ActiveRecord::Base.connection.tables.each do |table_name|
next if table_name == 'ar_internal_metadata'
next unless ActiveRecord::Base.connection.columns(table_name).map(&:name).include?('id')
ActiveRecord::Base.connection.execute "SELECT setval('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}));"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment