how do i reset a primary key id in ruby on rails 6?

The method does exist (as of Rails 6.0.0) for PostgreSQL:

ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements#reset_pk_sequence!

https://apidock.com/rails/v6.0.0/ActiveRecord/ConnectionAdapters/PostgreSQL/SchemaStatements/reset_pk_sequence%21

Note that you are using SQLite:

#<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x000000000af92360>

So your available methods are here: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SQLite3Adapter.html

More importantly, YOU SHOULDN’T RESET YOUR TABLE IDS JUST BECAUSE YOU DELETED A USER. The database can handle incrementing the IDs no problem, even after you delete a user.

I’m having a hard time coming up with a use-case that would require resetting the Db Ids after deleting a record.

If you REALLY want to do this: Reset the id field in a table in a sqlite db in Rails

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top