在没有默认值的列上使用 change_column_default 时,'from' 值应该是多少?

Wit*_*Wit 1 ruby-on-rails rails-migrations rails-activerecord

这是我的创建表迁移。请注意,我没有提供price.

class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.string :name
      t.decimal :price, precision: 8, scale: 2

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在我想设置一个默认值。根据迁移指南,我应该提供from使其可逆。我应该提供什么价值?

class ChangeProductsPriceDefault < ActiveRecord::Migration[5.0]
  def change
    change_column_default :products, :price, from: 'WHAT_TO_WRITE_HERE?', to: 0
  end
end
Run Code Online (Sandbox Code Playgroud)

Mik*_*iet 5

“无默认值”表示默认值为 NULL,因此:

change_column_default :products, :price, from: nil, to: 0
Run Code Online (Sandbox Code Playgroud)

有关于默认为 nil 的描述:http : //api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_column_default