ruby-on-rails - 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 中止!标准错误:发生错误,所有以后的迁移都取消了:
表“组”没有外键
请帮帮我。
解决方案
推荐阅读
- java - 如何使用 Retrofit 和 RxJava Android 发布应用程序/Json 类型
- perl - Perl 子程序不在循环中工作
- ios - iOS:如何检查扬声器是否打开
- java - Hibernate 抛出 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:检查手册以了解在“localTime”时间附近使用的正确语法
- java - foreach 循环中是否有任何空值检查?
- python - Django中模型继承的意外结果
- python - 如果句子以句点“。”结尾,如何编写与模式匹配的正则表达式
- javascript - 如何在javascript中使用手动单击项目符号创建自动幻灯片?
- amazon-web-services - 如何删除由 aws 联合用户创建的所有资源
- node.js - Exceljs写入文件