外国人 – 删除外键

我想在我的rails 4 app中使用mailboxer。 当我尝试部署数据库时出现问题。 创建邮箱会话表时出错,该表在通知表中具有依赖关系。

我正在尝试删除通知对话的外键。

我创建了一个迁移说:

change_table :notifications do |t| t.remove_foreign_key :conversations 

然而,rake中止并说外键不存在。

 rake aborted! An error has occurred, this and all later migrations canceled: PG::UndefinedObject: ERROR: constraint "notifications_conversation_id_fk" of relation "notifications" does not exist 

我的架构包括:add_foreign_key“notifications”,“conversation”,name:“notifications_on_conversation_id”

我试图挖掘db:migrate:down创建邮箱的原始迁移,但也收到错误,说’找不到命令’。

有人可以帮忙吗? 谢谢。

架构中的add_foreign_key命令为外键提供了名称notifications_on_conversation_id 。 此名称与外国人通常根据列名称分配的默认名称不同,后者是notifications_conversation_id_fk 。 因此,您的remove_foreign_key命令必须指定现有的外键名称而不是列名称。 尝试:

 remove_foreign_key :notifications, name: "notifications_on_conversation_id" 
 # Removes the given foreign key from the table. # Removes the foreign key on +accounts.branch_id+. remove_foreign_key :accounts, :branches # Removes the foreign key on +accounts.owner_id+. remove_foreign_key :accounts, column: :owner_id # Removes the foreign key named +special_fk_name+ on the +accounts+ table. remove_foreign_key :accounts, name: :special_fk_name 

官方文件: http : //api.rubyonrails.org/v4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_foreign_key