在Rails的sqlite数据库中的表中重置ID字段 [英] Reset the id field in a table in a sqlite db in Rails
问题描述
我在示例Rails应用程序中使用sqlite db.从我的用户表中,我已经使用 User.delete_all
清除了所有记录.但是现在,每当我使用 User.create
在表中插入一条新记录时,该ID的起始值都比该表中最后一条记录的ID多一个.例如,如果我的表有5条记录并且我清除了所有记录,那么当我执行User.create时,其起始于ID 6.有什么办法可以让id重新从1开始?
Im using sqlite db in a sample rails application. From my users table, i have cleared all records using User.delete_all
. But now whenever i insert a new record in the table using User.create
, the id is starting at a value which is one more than the id of the last record which was there in the table. For example, if my table had 5 records and i cleared all, then when i do User.create, its starting at id 6.
Is there any way i can make the id start from 1 again ?
谢谢
推荐答案
Similar question : How to reset a single table in rails? . We can run the following at rails console to reset id column to 1 for a sqlite table
ActiveRecord::Base.connection.execute("DELETE from sqlite_sequence where name = '<table_name>'")
这篇关于在Rails的sqlite数据库中的表中重置ID字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!