[英]Undo previously seeded data in Rails
I have seeded a row of data to my table by editing db/seed.rb
file and executing rake db:seed
command.我通过编辑db/seed.rb
文件并执行rake db:seed
命令将一行数据播种到我的表中。 Unknowingly, I put some wrong information in to that row.在不知不觉中,我在该行中输入了一些错误信息。 So I want to remove the previously added row of data.所以我想删除之前添加的数据行。 Is there any rake command for the same like rake db:rollback
for rake db:migrate
.是否有任何类似rake db:rollback
for rake db:migrate
的 rake 命令。
There are a couple of aspects to this: 这有几个方面:
1: You want to change the seed data when no other data is present in the database: 1:当数据库中没有其他数据时,您想要更改种子数据:
You should simply redo the rake db:seed
after updating the seed.rb file. 您应该在更新seed.rb文件后重做rake db:seed
。 Make sure you have MyModel.delete_all
before you try to add anything to that model. 在尝试向该模型添加任何内容之前,请确保您具有MyModel.delete_all
。
2: You want to change the seed data, but there are other data added to the database 2:您想要更改种子数据,但是还有其他数据添加到数据库中
This is a bit harder. 这有点难。 Often the simplest thing to do here is to manually change the data with either raw sql-statements, or with the use of tools like PhpPpAdmin , PhpMyAdmin etc. 经常在这里做最简单的事情是无论是原始SQL语句手动更改数据,或者使用的工具,如PhpPpAdmin , phpMyAdmin的等。
Now, there is possiby one way to hack this together, and that would be to do some voodoo in the seed.rb file. 现在,有可能一种方法来破解这一点,那就是在seed.rb文件中做一些伏都教。 So you could run rake db:seed deseed=true
, then in your seed.rb: 所以你可以运行rake db:seed deseed=true
,然后在你的seed.rb中:
if ENV['deseed']
#Do your deseeding action here
else
#Do your seeding here.
end
You could even get real crazy and do something like this: 你甚至可以变得疯狂,并做这样的事情:
deseed = ENV['desee']
#DANGER: Dirty hacks upcoming:
deseed? myModelCall = MyModel.method(:destroy_all): myModelCall = MyModel.method(:create)
myModelCall.call :user_id_or_whatevs => 23 #this creates or deletes a MyModel entity with the given parameters
#NOTE this might not work in all cases and I would not necessarily recommend doing this.
#<3uby
I had similar issues when I seeded my data.当我播种我的数据时,我遇到了类似的问题。 In fact, I ran the seed command twice and I couldn't find a way to revoke the second seed.事实上,我运行了两次种子命令,但找不到撤销第二个种子的方法。 However, I had to run rails db:reset
command and then run the rails db:seed
command again and that fixed the problem for me.但是,我必须运行rails db:reset
命令,然后再次运行rails db:seed
命令,这为我解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.