2017-03-08 113 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