首页 > 解决方案 > StandardError:发生错误,所有后续迁移都取消:Rails migration error for remove Foreign keys

问题描述

我正在尝试将外键添加到表中已经存在的 id 并将迁移运行为:

rails g migration AddForeignKeysRefToQuotes

我打开了迁移文件并添加了:

class AddForeignKeysRefToQuotes < ActiveRecord::Migration[5.2]
  def change
    add_foreign_key :quotes, :groups,name: "index_quotes_on_group_id"
    add_foreign_key :quotes, :users, column: :created_by_id, name: "index_quotes_on_created_by_id"
    add_foreign_key :quotes, :template, column: :work_template_id, name: "index_quotes_on_work_template_id"
  end
end

在上面的文件中,我为 work_template_id 添加了错误的表名,所以在运行时

rake db:migrate 它给出了一个错误,但是除了第三个之外,其余列 group_id 和 created_by_id 被创建。我找不到该文件,但我可以在 Schema.rb 和 mysql 中看到外键。

我尝试运行rails g migration RemoveForeignKeysRefFromQuotes 并在删除迁移中添加以下内容:

class RemoveForeignKeysReFromQuotes < ActiveRecord::Migration[5.2]
  def change
    remove_foreign_key :quotes, :groups, name: :index_quotes_on_group_id
    remove_foreign_key :quotes, :users, column: :created_by_id, name: :index_quotes_on_created_by_id
  end
end

但我收到错误消息:

== 20181024060431 RemoveForeignKeysReFromQuotes: 迁移 ==================== -- remove_foreign_key(:groups) rails 中止!标准错误:发生错误,所有以后的迁移都取消了:

表“组”没有外键

请帮帮我。

标签: ruby-on-railsrails-migrations

解决方案


推荐阅读