【问题标题】:migration not rolling back迁移不回滚
【发布时间】:2016-05-17 12:23:37
【问题描述】:

我已经运行了这个迁移:

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

现在我正在尝试回滚,但它显示错误:

StandardError:发生错误,此迁移和所有后续迁移 取消:remove_index 只有在给定 :column 选项时才可逆。

如何将此迁移回滚到以前的版本?

【问题讨论】:

  • 试试改成remove_index :locations, column: :name
  • 我认为现在您必须使用 remove_index 方法从迁移中手动删除位置和名称的索引。为此,您可以创建新的迁移或更改为 up 和 down 状态。
  • 非常感谢。有效。只需指定“列::名称”,而不仅仅是“名称”:)

标签: ruby-on-rails rollback rails-migrations


【解决方案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

【讨论】:

  • 它仍然不起作用..max的解决方案有效。只需将“:name”更改为“column::name”。 :) 我根据您的代码编辑了我的代码。所以两者都有帮助;)
猜你喜欢
  • 2014-06-21
  • 2012-08-27
  • 2021-07-01
  • 1970-01-01
  • 2018-09-14
  • 2018-10-31
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
相关资源
最近更新 更多