2017-03-08 30 views
1

我嘗試將默認列值從false更改爲true。但是當我運行rake db:migrate VERSION=904984092840298時,出現以下錯誤。使用遷移更改表列的默認值

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 
' 

遷移

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

回答

1

令人奇怪的是,因爲根據文檔(change_column_default)你的代碼應該工作..

正如你可以定義updown一個選項:

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 
+0

非常感謝!這是工作! :) – Lory

+0

@Lory,但奇怪的是,你的原始代碼沒有,因爲它看起來正確根據文檔 –

+0

這對我來說也很奇怪,因爲我完全按照文檔編寫它。 – Lory