简体   繁体   English

如何使用rails命令恢复rails控制台中已经删除的数据?

[英]How to restore the data which are already deleted DB in rails console by using rails commands?

I deleted some data in a database but I want those data back.我删除了数据库中的一些数据,但我想找回这些数据。

Is there any way to restore the data from a database that was deleted using the rails console?有没有办法从使用 rails 控制台删除的数据库中恢复数据?

不幸的是,除非先前删除的对象已保存到变量或其他地方,否则您无法使用控制台工具恢复数据。

No, you cannot recover any data that was deleted using the rails console.不,您无法恢复使用 rails 控制台删除的任何数据。 You will need a backup.您将需要备份。 If you don't have a backup, you will not be able to restore the data.如果您没有备份,您将无法恢复数据。

However, depending on how you removed your data, you might have had a chance to recover it at the moment you deleted it.但是,根据您删除数据的方式,您可能有机会在删除时恢复它。

If you used ActiveRecord code like the following如果您使用了如下所示的 ActiveRecord 代码

Rails.application.eager_load!
ActiveRecord::Base.connection.disable_referential_integrity do
  ApplicationRecord.descendants.each do |model|
    model.delete_all
  end
end

The data is lost.数据丢失。 (Have a look at this question and the answers about different ways to truncate data) (看看这个问题和有关截断数据的不同方法的答案)

If you did something like this:如果你做了这样的事情:

objects = MyModel.delete_all

then you might have had a chance to recover them right after that using the objects array.那么您可能有机会在使用objects数组之后立即恢复它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM