2013-03-15 57 views
2

我在一個git分支上,試圖在銷燬分支之前回滾兩個遷移。最近的遷移將一列添加到我想要保留的表中(並且哪些是master的一部分,而不是要刪除的分支),因此刪除整個表並不是解決方案(除非我必須重新創建它)。無論如何,我一定犯了錯誤,因爲當我試圖從scores表中刪除apple_id列時,我得到了這個中止錯誤。索引名稱太長,無法回滾遷移

這是我想回滾

add_column :scores, :apple_id, :integer 

但是遷移,錯誤信息(見下文)是指的是我與原遷移(主分支的一部分)中創建的索引是創建表格

add_index :scores, [:user_id, :created_at, :funded, :started] 

你能建議我可能做什麼嗎?

== AddAppleidColumnToScores: reverting ======================================= 
-- remove_column("scores", :apple_id) 
rake aborted! 
An error has occurred, this and all later migrations canceled: 

Index name 'temp_index_altered_scores_on_user_id_and_created_at_and_funded_and_started' on table 'altered_scores' is too long; the limit is 64 characters 

更新:閱讀這太問題How do I handle too long index names in a Ruby on Rails migration with MySQL?,我得到了這個問題的根源一些更多的信息,但不知道如何解決它。 SQL和Postgres的有64個字符的限制

Index name 'index_studies_on_user_id_and_university_id_and_subject_\ 
      name_id_and_subject_type_id' on table 'studies' is too long; \ 
      the limit is 64 characters 

爲我指的是說給索引的名稱問題接受的答案,雖然我不知道我怎麼能現在,我試圖做到這一點回滾。

add_index :studies, ["user_id", "university_id", \ 
      "subject_name_id", "subject_type_id"], 
      :unique => true, :name => 'my_index' 

更新:在回覆評論時,我使用了Rails 3.2.12。這是將此列

class AddAppleidColumnToScores < ActiveRecord::Migration 
    def change 
    add_column :scores, :apple_id, :integer 
    end 
end 

而且,爲什麼我沒有要刪除該表是我不確定它可能在重新創建因爲)的主要部分是造成什麼問題的原因遷移在分支主機上創建,而在分支上添加列,b)我不確定如何處理刪除的表的遷移文件?因爲它是我創建的第四張(大約10張)桌子,我不知道如何運行它,只能再次運行它。

+0

什麼版本的Rails,您使用的?我用Rails v3.1.0從零開始重新創建了一個項目,但回滾效果不錯。我也在使用PG v9.0.1。當我創建一個索引爲'add_index:scores,[:user_id,:created_at,:funded,:started]'時,索引名稱爲'index_scores_on_user_id_and_created_at_and_funded_and_started'。 – 2013-03-15 22:39:26

+0

如果您不想從頭開始刪除並重新創建數據庫,我強烈建議您在數據庫控制檯中手動刪除索引。而且,你可以在回滾之前在這裏粘貼你的完整遷移'AddAppleIdColumnToScores'和你的schema.rb嗎? – 2013-03-15 22:40:12

+0

您是否嘗試使用純SQL刪除索引? – 2013-03-18 09:24:18

回答

0

您可以複製/粘貼您的遷移嗎?

這裏是我的:

class AddAppleIdColumnToScores < ActiveRecord::Migration 

    def self.up 
    add_column :scores, :apple_id, :integer 
    add_index :scores, [:user_id, :created_at, :funded, :started] 
    end 

    def self.down 

    # delete index MySql way 
    # execute "DROP INDEX index_scores_on_user_id_and_created_at_and_funded_and_started ON scores" 

    # delete index Postgresql way 
    # execute "DROP INDEX index_scores_on_user_id_and_created_at_and_funded_and_started" 

    # delete index Rails way/not necessary if you remove the column 
    # remove_index :scores, [:user_id, :created_at, :funded, :started] 

    remove_column :scores, :apple_id 
    end 
end 
+0

我用Rails v(3.2.12)和遷移更新了OP。我猜我應該放下桌子。我該怎麼做,以及重新創建它的最佳方式是什麼?我是否進行新的遷移或僅遷移創建該表的遷移?這種遷移不是最近的嗎? (即它是關於我創建的第四次遷移,現在我已經完成了大約10次,如果我刪除表並嘗試重新創建,最後六次會發生什麼?) – Leahcim 2013-03-16 00:08:32

+0

我建議您添加一個新遷移以刪除表if需要。也許你應該爲down方法(回滾)提出一個'ActiveRecord :: IrreversibleMigration'。但是,如果您認爲自己在遷移過程中遇到了太多麻煩,那麼可以考慮以一種乾淨的方式重新創建完整的數據庫。 – 2013-03-18 09:23:50