Skip to content

Instantly share code, notes, and snippets.

@Disha-Shah
Forked from paulsturgess/reset_primary_key.md
Last active December 9, 2016 07:40
Show Gist options
  • Save Disha-Shah/14ddd0470d95da7304ab94b508274fd1 to your computer and use it in GitHub Desktop.
Save Disha-Shah/14ddd0470d95da7304ab94b508274fd1 to your computer and use it in GitHub Desktop.
Reset postgres primary key index using Rails
$ ActiveRecord::Base.connection.reset_pk_sequence!('table_name')

If you need the table names:

$ ActiveRecord::Base.connection.tables
=> ["accounts", "assets", ...]

If you need to reset for all tables:

 $ all_tables = ActiveRecord::Base.connection.tables
 => ["accounts", "assets", ...]

 $ all_tables.each do |table|
 $   ActiveRecord::Base.connection.reset_pk_sequence!(table)
 $ end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment