2017-01-10 176 views
1

我有這個遷移在我的Rails項目刪除列默認

class ChangeColumnDefaultOfTemp < ActiveRecord::Migration 
    def up 
    change_column_null :states, :temp, false, '' 
    change_column_default :states, :temp, '' 
    end 

    def down 
    change_column_null :states, :temp, true 
    change_column :states, :temp, :string, default: nil 
    end 
end 

我能夠與

change_column_null :states, :temp, true 

恢復NOT NULL約束,但這個命令

change_column :states, :temp, :string, default: nil 

是不是給我預期的結果

rake db:migrate 

temp | character varying(255) | not null default ''::character varying 

rake db:rollback 


temp | character varying(255) | default NULL::character varying 

-- Expected is 
temp | character varying(255) | 

注:尋找類似的東西,以

ALTER [ COLUMN ] column DROP DEFAULT 

ref

回答

0

我認爲這是在Rails中常見的錯誤,當你有一個string類型列。我只想跟原始的SQL

class ChangeColumnDefaultOfTemp < ActiveRecord::Migration 
    # ... 
    def down 
    execute <<-SQL 
     ALTER TABLE states ALTER COLUMN temp DROP DEFAULT; 
    SQL 
    end 
end