Nikita Kazakov
Nikita Kazakov
~1 min read


If you import data into a Rails project manually, you’ll see that the primary key count in Rails might be off. For example, running User.last doesn’t show the last row in the User table.

The problem is that the primary key sequence is off. This value is maintained by Postgresql every time you add a new row.

The fix this problem, enter Rails console.

Let’s say you know that it’s the users table where you want to reset the primary key sequence. Run:


If you want to reset the primary key sequence for all tables in the database, run:

ActiveRecord::Base.connection.tables.each do |table|