Reversible migration for change_column_default from not having any default in Rails

前端 未结 7 2200
青春惊慌失措
青春惊慌失措 2021-02-18 21:34

The Rails guides to active record migrations says that you can do

change_column_default :products, :approved, from: true, to: false

I\'ve got a

7条回答
  •  失恋的感觉
    2021-02-18 22:30

    When you try to downgrade the data type , from value (0) to nil in this case , rails will complain this error. Because it might lose data

    Another example would be going from string -> integer.

    this is a really good article explain the same

    UPDATE

    It seems like your reverse migration calls change_column_default in command_recorder (activerecord-4.2.0/lib/active_record/migration/command_recorder.rb#76) please check if you set the nil as the default value.

    UPDATE2

    it turns out that, if you use change in migrations, those have to be able to reversible

    but change_column is not reversible through change method. So you will have to use the old rails migration method with up and down

    read this article explain the scenario

提交回复
热议问题