Ruby on rails 活动记录中不可逆迁移的好处?

Ruby on rails 活动记录中不可逆迁移的好处?,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我一直在阅读rails中的活动记录迁移。大多数情况下,我们可能会进行可逆迁移,但我不明白进行不可逆迁移的意义何在?有人能举一些例子吗?让它们不可逆转有什么好处 以下是我在rails指南中读到的内容: class ChangeProductsPrice < ActiveRecord::Migration def change reversible do |dir| change_table :products do |t| dir.up { t.c

我一直在阅读rails中的活动记录迁移。大多数情况下,我们可能会进行可逆迁移,但我不明白进行不可逆迁移的意义何在?有人能举一些例子吗?让它们不可逆转有什么好处

以下是我在rails指南中读到的内容:

class ChangeProductsPrice < ActiveRecord::Migration
  def change
    reversible do |dir|
      change_table :products do |t|
        dir.up   { t.change :price, :string }
        dir.down { t.change :price, :integer }
      end
    end
  end
end
class ChangeProductsPrice
假设您的表
addresses
中有一列名为
zipcode
,其类型当前为
integer
,您希望将其更改为
string

现在,很有可能将数据类型从
integer
更改为
string
,但是将
string
更改回整数并不总是可行的,因此我们无法恢复此迁移

def self.up
  change_column :address, :zipcode, :string
end 
现在,如果您执行
rake db:migrate
,它将正常工作,但是当您要恢复它时,您将返回以下错误:

--雷克流产了! --ActiveRecord::不可逆迁移


让它们不可逆转有什么好处

事实上,我们并不想让它们不可逆转;它碰巧是不可逆转的。就个人而言,我会选择“始终可逆”,因为它们非常灵活