Reversible migration for change_column_default from not having any default in Rails

前端 未结 7 2230
青春惊慌失措
青春惊慌失措 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:22

    Using from and to was added in Rails 5+

    The guide linked to in the question is for Edge Rails, not for a released version of Rails.

    Reversible syntax for change_column_default is the result of pull request 20018. The pull request also updated the Rails guides for Edge Rails.

    From activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:

    -      def change_column_default(table_name, column_name, default)
    +      # Passing a hash containing +:from+ and +:to+ will make this change
    +      # reversible in migration:
    +      #
    +      #   change_column_default(:posts, :state, from: nil, to: "draft")
    +      #
    +      def change_column_default(table_name, column_name, default_or_changes)
    

    The pull request was made June 27, 2015, which is more recent than any version of Rails released as of August 1, 2015.

    The documentation for migration for Rails 4.2.3 reflects the fact that reversible syntax is not yet available:

    change_column_default :products, :approved, false
    

提交回复
热议问题