foreigner - remove foreign key

Mel picture Mel · May 18, 2014 · Viewed 33.8k times · Source

I am trying to use mailboxer in my rails 4 app. A problem is arising when i try to deploy the db. The error occurs in creating the mailboxer conversations table, which has dependencies in notifications table.

I am trying to remove the foreign key for notifications conversations.

I created a migration which says:

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

However, the rake aborts and says a foreign key does not exist.

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

My schema includes: add_foreign_key "notifications", "conversations", name: "notifications_on_conversation_id"

I tried to rake db:migrate:down the original migration that created mailboxer, but also got an error saying 'command not found'.

Can anyone help? Thank you.

Answer

cschroed picture cschroed · May 18, 2014

The add_foreign_key command in your schema gave your foreign key the name notifications_on_conversation_id. This name is different than the default name that foreigner would normally assign based on the column name, which is notifications_conversation_id_fk. So your remove_foreign_key command must specify the existing foreign key name instead of the column name. Try:

remove_foreign_key :notifications, name: "notifications_on_conversation_id"