2016-05-17 90 views
2

我已經運行這個遷移:遷移不會回滾

class AddUniqueToLocationColumnName < ActiveRecord::Migration 
    def change 
    remove_index :locations, :name 
    add_index :locations, :name, unique: true 
    end 
end 

,現在我想回滾,但其示值誤差:

StandardError: An error has occurred, this and all later migrations canceled: remove_index is only reversible if given a :column option.

我怎麼可以回滾此遷移到我以前的版本?

+0

嘗試將其更改爲'remove_index:位置,柱:name' – max

+0

我想現在你必須從你的遷移中手動刪除位置和名稱索引使用remove_index方法。爲此,您可以創建新遷移或更改爲向上和向下狀態。 –

+0

非常感謝。有效。只需要指定「列::名稱」,而不是隻有「名稱」:) – Abhishek

回答

1

儘量明確定義上下:

class AddUniqueToLocationColumnName < ActiveRecord::Migration 
    def self.up 
    remove_index :locations, column: :name 
    add_index :locations, :name, unique: true 
    end 

    def self.down 
    remove_index :locations, column: :name # remove unique index 
    add_index :locations, :name # adds just index, without unique 
    end 
end 
+1

它仍然不會work..max的解決方案工作。只需將「:name」更改爲「column :: name」即可。 :),我根據你的代碼編輯我的代碼。所以都幫助;) – Abhishek

+0

不錯!我用@max解決方案編輯了我的答案 –