StandardError:发生错误,所有后来的迁移都被取消:删除外键的Rails迁移错误

时间:2018-10-24 07:14:19

标签: ruby-on-rails rails-migrations

我正在尝试将外键添加到表中已存在的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)导轨被中止了! StandardError:发生错误,所有以后的迁移都被取消:

     

表“组”没有外键

请帮帮我。

0 个答案:

没有答案