简体   繁体   中英

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?

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

No, you cannot recover any data that was deleted using the rails console. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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