Ruby-on-rails 使用 rails 控制台截断表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13173618/
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
Truncate table(s) with rails console
提问by CaptainCarl
I have this testingdatabase which, by now, is stuffed with junk. Now I've done a few Table.destroy_all commands in the rails console which deletes all records and dependencies which is awesome. However; I'd like to truncate everything so the ID's etc. start at 1 again. Is there any way in Rails 3?
我有这个测试数据库,现在它塞满了垃圾。现在我已经在 rails 控制台中完成了一些 Table.destroy_all 命令,它删除了所有记录和依赖项,这很棒。然而; 我想截断所有内容,以便 ID 等再次从 1 开始。Rails 3 中有什么办法吗?
回答by Ravi Sankar Raju
The accepted answer only works if you need to recreate the whole database.
To drop a single table (with the callbacks) and to get the IDs to start from 1:
接受的答案仅在您需要重新创建整个数据库时才有效。
删除单个表(带有回调)并使 ID 从 1 开始:
Model.destroy_all # Only necessary if you want to trigger callbacks.
ActiveRecord::Base.connection.execute("TRUNCATE #{table_name} RESTART IDENTITY")
If you are using Sqlite, it does not support truncate so do the following:
如果您使用的是 Sqlite,它不支持截断,请执行以下操作:
Model.destroy_all # Only necessary if you want to trigger callbacks.
ActiveRecord::Base.connection.execute("Delete from #{table_name}")
ActiveRecord::Base.connection.execute("DELETE FROM SQLITE_SEQUENCE WHERE name='#{table_name}'")
回答by Mr.Quack
This worked for me -
ActiveRecord::Base.connection.execute("TRUNCATE table_name")
这对我有用-
ActiveRecord::Base.connection.execute("TRUNCATE table_name")
回答by Carlos Luis Rojas Aragonés
Model.connection.truncate(Model.table_name)
回答by jsears
Assuming you're using MySQL or Postgre and not SQlite3 (which doesn't support TRUNCATE), you could do the following:
假设您使用的是 MySQL 或 Postgre 而不是 SQlite3(不支持TRUNCATE),您可以执行以下操作:
MyModel.connection_pool.with_connection { |c| c.truncate(MyModel.table_name) }
Note that this would not invoke ActiveRecord callbacks.
请注意,这不会调用 ActiveRecord 回调。
回答by Yam Marcovic
Simply rebuild the database on the next test run (this will happen automatically after dropping it).
只需在下一次测试运行时重建数据库(这将在删除它后自动发生)。
rake db:drop RAILS_ENV=test
rake db:drop RAILS_ENV=test
回答by Papouche Guinslyzinho
You could also do rake db:rollback STEP=3 RAILS_ENV=test
你也可以这样做 rake db:rollback STEP=3 RAILS_ENV=test
where 3 represents the number of migrations that you have in db/migrate. In example: If I have in
其中 3 表示您在 db/migrate 中的迁移次数。例如:如果我有
db/migrate
20140121065542_create_users.rb
20140121065710_create_profiles.rb
20140121065757_create_articles.rb
20140121065900_create_comments.rb
20140121065929_create_categories.rb
So I have 5 migrations in total to remove. If I do rake db:rollback STEP=5 RAILS_ENV=testall the tables will be drop from my TEST database and if I remove RAILS_ENV=test than all the ENVIRONNMENT (production, test, development) tables will be delete and it cleans also db/shema.rb file from it's migration datas.
所以我总共有 5 个迁移要删除。如果我这样做,rake db:rollback STEP=5 RAILS_ENV=test所有的表都将从我的 TEST 数据库中删除,如果我删除 RAILS_ENV=test,那么所有 ENVIRONNMENT(生产、测试、开发)表都将被删除,它还会从它的迁移数据中清除 db/shema.rb 文件。
回答by Eva
回答by Justin E
(A bit late to the party, I know)
(聚会有点晚,我知道)
Asking to do this in the console:
要求在控制台中执行此操作:
2.1.2 :001 > Post.all.each do |post|
2.1.2 :002 > post.destroy!
2.1.2 :003 > end
Works as well...
也有效...
This essentially loops through all of the posts and destroys them. It does not change your auto increment value though...
这基本上会遍历所有帖子并销毁它们。虽然它不会改变你的自动增量值......
Same logic should work for Rails 3 as well (though I am using Rails 4)
同样的逻辑也适用于 Rails 3(虽然我使用的是 Rails 4)

