2013-03-15 62 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