Change the default value for table column with migration

Lory picture Lory · Mar 8, 2017 · Viewed 10.7k times · Source

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

Answer

Andrey Deineko picture Andrey Deineko · Mar 8, 2017

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