这是我的创建表迁移。请注意,我没有提供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
现在我想设置一个默认值。根据《迁移指南》,我应该提供from
以使其可逆。我应该提供什么价值?
class ChangeProductsPriceDefault < ActiveRecord::Migration[5.0]
def change
change_column_default :products, :price, from: 'WHAT_TO_WRITE_HERE?', to: 0
end
end
" no默认"表示默认值是null,所以:
change_column_default :products, :price, from: nil, to: 0