I want the primary key values to start from 1 again.
Resetting primary key sequence in Rails can be achieved by simply calling method reset_pk_sequence! on ActiveRecord with table_name .
A lot of people (like me) come here to find how to delete all the data in the table. Here you go:
$ rails console > ModelName.delete_all
or
> ModelName.destroy_all
destroy_all checks dependencies and callbacks, and takes a little longer. delete_all is a straight SQL query.
More info here: http://apidock.com/rails/ActiveRecord/Base/delete_all/class
I've been using the following from rails console to delete everything in the table and then reset the index counter (Ruby 2 & Rails 4):
> ModelName.delete_all > ActiveRecord::Base.connection.reset_pk_sequence!('plural_model_name')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With