2

我在一个 git 分支上并试图在销毁分支之前回滚两个迁移。最近的迁移将一列添加到我想要保留的表中(并且它是 master 的一部分,而不是要删除的分支),因此删除整个表不是解决方案(除非我必须再次重新创建它)。无论如何,我一定做错了什么,因为当我试图从分数表中删除 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

更新:阅读这个 SO 问题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

此外,我不想删除表的原因是我不确定在重新创建它时可能会导致什么问题,因为 a) 主要部分是在分支 master 上创建的,而在分支上添加了一列 b)我不确定如何处理已删除表的迁移文件?因为它是我创建的第四个(大约 10 个)表,所以我不知道如何运行它,只能再次运行它。

4

1 回答 1

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
于 2013-03-15T22:51:18.717 回答