Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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", ...]
@holamendi

This comment has been minimized.

Copy link

@holamendi holamendi commented Dec 12, 2017

It's been 5 years and this is still useful. Thanks! 🥇

@tomav

This comment has been minimized.

Copy link

@tomav tomav commented May 3, 2018

Combine both of them:

        ActiveRecord::Base.connection.tables.each do |t|
          ActiveRecord::Base.connection.reset_pk_sequence!(t)
        end
@tashi-179

This comment has been minimized.

Copy link

@tashi-179 tashi-179 commented Nov 25, 2019

This is it. Perfect answer.

@kinoute

This comment has been minimized.

Copy link

@kinoute kinoute commented Jun 7, 2020

2020, still great. thanks!

@Mozart2234

This comment has been minimized.

Copy link

@Mozart2234 Mozart2234 commented Sep 30, 2020

2020, still great. thanks!

Yep!!

@Rukosenpa

This comment has been minimized.

Copy link

@Rukosenpa Rukosenpa commented Jul 1, 2021

2021, here for get help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment