Search code examples
ruby-on-railsrubyruby-on-rails-4activerecordruby-on-rails-5

Change the default value for table column with migration


I try to change the default column value from false to true. But when I run rake db:migrate VERSION=904984092840298 I got the following ERROR.

StandardError: An error has occurred, this and all later migrations canceled:

PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type boolean: "---
:from: false
:to: true
"
: ALTER TABLE "plussites" ALTER COLUMN "hide_season_selector" SET DEFAULT '---
:from: false
:to: true
'

Migration

class ChangeDefaultvalueForHideSeasonSelector < ActiveRecord::Migration 
  def change 
    change_column_default :plussites, :hide_season_selector, from: false, to: true 
  end
end

Solution

  • It is strange, because according to documentation (change_column_default) your code should work..

    As an option you might define up and down:

    class ChangeDefaultvalueForHideSeasonSelector < ActiveRecord::Migration
      def up
        change_column_default :plussites, :hide_season_selector, true
      end
    
      def down
        change_column_default :plussites, :hide_season_selector, false
      end
    end