Change the default value for table column with migration
Asked Answered
K

2

41

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
Kit answered 8/3, 2017 at 10:10 Comment(0)
I
45

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
Isidore answered 8/3, 2017 at 10:12 Comment(6)
@Kit but it is strange, that your original code did not, because it looks correct according to documentationIsidore
It was strange also for me, because I wrote it exactly according to the documentation.Kit
@ Andrey Deineko Yes, this project is pretty old but was involved today on it. Maybe this is the issue.Kit
@Kit so this is most likely it :)Isidore
@Kit btw, next time always add the env information to the question, like Ruby/Rails versions - it is important ;)Isidore
@Andray Deineko I will take your advice. Thank you! Спасиба :)Kit
E
69

You have to check which version of ActiveRecord you are using. According to your command rake db:migrate you are still on Ruby on Rails 4.2 or earlier.

If you are on ActiveRecord up to 4.2 (change_column_default 4.2.9), there is no from/to option and you can define only the new default option as param.

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

The solution above won't allow a rollback as the method don't know, what the previous default value was. This is why you have to define an separate up and down method:

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

If you are on Ruby on Rails 5 or newer, there are new possibilities to define the value which was before and which one should be after by from/to (change_column_default 5.0.0.1). On Ruby on Rails 5 you can use your chosen solution:

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

I hope this explanation will help the people with comments under the other answer.

Echovirus answered 7/6, 2019 at 8:12 Comment(0)
I
45

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
Isidore answered 8/3, 2017 at 10:12 Comment(6)
@Kit but it is strange, that your original code did not, because it looks correct according to documentationIsidore
It was strange also for me, because I wrote it exactly according to the documentation.Kit
@ Andrey Deineko Yes, this project is pretty old but was involved today on it. Maybe this is the issue.Kit
@Kit so this is most likely it :)Isidore
@Kit btw, next time always add the env information to the question, like Ruby/Rails versions - it is important ;)Isidore
@Andray Deineko I will take your advice. Thank you! Спасиба :)Kit

© 2022 - 2024 — McMap. All rights reserved.