Ruby-on-rails 如何重置 rails 中的单个表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset a single table in rails?
提问by yukas
I want the primary key values to start from 1 again.
我希望主键值再次从 1 开始。
采纳答案by Rick
To reset the index/primary key in SQLite just type:
要在 SQLite 中重置索引/主键,只需键入:
$ rails console
> ActiveRecord::Base.connection.execute("DELETE from sqlite_sequence where name = 'yourtablename'")
回答by Flaviu
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.
destroy_all 检查依赖项和回调,需要更长的时间。delete_all 是一个直接的 SQL 查询。
More info here: http://apidock.com/rails/ActiveRecord/Base/delete_all/class
更多信息:http: //apidock.com/rails/ActiveRecord/Base/delete_all/class
回答by Meltemi
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):
我一直在使用 rails 控制台中的以下内容来删除表中的所有内容,然后重置索引计数器(Ruby 2 和 Rails 4):
> ModelName.delete_all
> ActiveRecord::Base.connection.reset_pk_sequence!('plural_model_name')
回答by vansan
@khelll's link is helpful. The command you want to truncate one table is:
@khell 的链接很有帮助。要截断一张表的命令是:
ActiveRecord::Base.connection.execute("TRUNCATE #{table_name}")
回答by BoraMa
Since Rails 4.2 you can use truncatedirectly on an ActiveRecord connection:
从 Rails 4.2 开始,您可以truncate直接在ActiveRecord 连接上使用:
ActiveRecord::Base.connection.truncate(:table_name)
This wipes all dataand resets the autoincrement countersin the table. Works in MySQL and Postgres, does notwork in Sqlite.
这会擦除所有数据并重置表中的自动增量计数器。适用于 MySQL 和 Postgres,不适用于 Sqlite。
回答by kikito
Add gem 'database_cleaner'to your Gemfile, run $ bundle install, and then:
添加gem 'database_cleaner'到您的 Gemfile,运行$ bundle install,然后:
> DatabaseCleaner.clean_with(:truncation, :only => ['yourtablename'])
You can specify more tables:
您可以指定更多表:
> DatabaseCleaner.clean_with(:truncation, :only => ['table1', 'table2', 'table3'])
If you leave the last parameter out, it will truncate the whole database:
如果省略最后一个参数,它将截断整个数据库:
> DatabaseCleaner.clean_with(:truncation) # your database is truncated
回答by skplunkerin
I'm using Rails 4.2.0 and Sqlite3
我正在使用 Rails 4.2.0 和 Sqlite3
Here's what worked for me (taking a bit from all of the above):
这是对我有用的(从以上所有内容中获取一点):
$ rails c
> ModelName.delete_all
> ActiveRecord::Base.connection.execute("DELETE from sqlite_sequence where name = 'table_name'")
I was then able to add new records to my table with the index starting back at 1
然后我就可以将新记录添加到我的表中,索引从 1 开始
回答by Matt Long
For anyone else looking for the answer to this question when the database is Postgres, you can do this from the Rails console:
对于在数据库为 Postgres 时寻找此问题答案的其他人,您可以从 Rails 控制台执行此操作:
rails console
irb(main):028:0> ActiveRecord::Base.connection.execute("SELECT SETVAL('accounts_id_seq', 1)")
Where the accountsin the accounts_id_seqis the name of the table.
其中accountsinaccounts_id_seq是表的名称。

