Rails中重置PostgreSQL 自增主键的值

重置单个表 在rails console中执行以下代码:

1
2
3
4
5
table = 'my_table'
auto_inc_val = 10 # New auto increment start point
ActiveRecord::Base.connection.execute(
"ALTER SEQUENCE #{table}_id_seq RESTART WITH #{auto_inc_val}"
)

重置所有表 在rails console中执行以下代码:

1
2
3
4
5
6
7
8
ActiveRecord::Base.connection.tables.each do |table|
result = ActiveRecord::Base.connection.execute("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1")
if result.any?
ai_val = result.first['id'].to_i + 1
puts "Resetting auto increment ID for #{table} to #{ai_val}"
ActiveRecord::Base.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{ai_val}")
end
end