Ruby-on-rails 重置数据库(全部清除),然后为数据库设定种子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4003978/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:27:45  来源:igfitidea点击:

Reset the database (purge all), then seed a database

ruby-on-railsdatabaseseeding

提问by Blankman

Is there a rake command to wipe out the data in the database tables?

是否有 rake 命令来清除数据库表中的数据?

How do I create a db:seed script to pre-fill data to my tables?

如何创建 db:seed 脚本以将数据预填充到我的表中?

回答by HymanCA

I use rake db:resetwhich drops and then recreates the database and includes your seeds.rb file. http://guides.rubyonrails.org/migrations.html#resetting-the-database

我使用rake db:resetwhich 删除然后重新创建数据库并包含您的 seed.rb 文件。 http://guides.rubyonrails.org/migrations.html#resetting-the-database

回答by laffuste

You can delete everything and recreate database + seeds with both:

您可以删除所有内容并重新创建数据库 + 种子:

  1. rake db:reset: loads from schema.rb
  2. rake db:drop db:create db:migrate db:seed: loads from migrations
  1. rake db:reset: 从 schema.rb 加载
  2. rake db:drop db:create db:migrate db:seed: 从迁移加载

Make sure you have no connections to db (rails server, sql client..) or the db won't drop.

确保您没有连接到 db(rails 服务器、sql 客户端 ..),否则 db 不会丢失。

schema.rb is a snapshot of the current state of your database generated by:

schema.rb 是由以下生成的数据库当前状态的快照:

rake db:schema:dump

回答by Tom Hundt

If you don't feel like dropping and recreating the whole shebang just to reload your data, you could use MyModel.destroy_all(or delete_all) in the seed.db file to clean out a table before your MyModel.create!(...)statements load the data. Then, you can redo the db:seedoperation over and over. (Obviously, this only affects the tables you've loaded data into, not the rest of them.)

如果您不想删除并重新创建整个shebang 只是为了重新加载您的数据,您可以在seed.db 文件中使用MyModel.destroy_all(或delete_all)在您的MyModel.create!(...)语句加载数据之前清除表。然后,您可以db:seed一遍又一遍地重做操作。(显然,这只会影响您加载数据的表,而不影响其余的表。)

There's a "dirty hack" at https://stackoverflow.com/a/14957893/4553442to add a "de-seeding" operation similar to migrating up and down...

https://stackoverflow.com/a/14957893/4553442上有一个“肮脏的黑客”来添加类似于上下迁移的“去种子”操作......

回答by DazBaldwin

As of Rails 5, the rakecommandline tool has been aliased as railsso now

从 Rails 5 开始,rake命令行工具的别名已经变成了rails现在这样

rails db:resetinstead of rake db:reset

rails db:reset代替 rake db:reset

will work just as well

也能正常工作

回答by Nesha Zoric

You can use rake db:resetwhen you want to drop the local database and start fresh with data loaded from db/seeds.rb. This is a useful command when you are still figuring out your schema, and often need to add fields to existing models.

您可以使用rake db:reset,当你要删除的本地数据库,并从加载的数据重新开始db/seeds.rb。当您仍在弄清楚您的架构并且经常需要向现有模型添加字段时,这是一个有用的命令。

Once the reset command is used it will do the following: Drop the database: rake db:dropLoad the schema: rake db:schema:loadSeed the data: rake db:seed

使用重置命令后,它将执行以下操作:删除数据库:rake db:drop加载架构:rake db:schema:load播种数据:rake db:seed

But if you want to completely drop your database you can use rake db:drop. Dropping the database will also remove any schema conflicts or bad data. If you want to keep the data you have, be sure to back it up before running this command.

但是如果你想完全删除你的数据库,你可以使用rake db:drop. 删除数据库还将删除任何架构冲突或错误数据。如果您想保留您拥有的数据,请务必在运行此命令之前对其进行备份。

This is a detailed article about the most important rake database commands.

这是一篇关于最重要的rake 数据库命令的详细文章。