Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreigner - remove foreign key

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.

like image 345
Mel Avatar asked May 18 '14 03:05

Mel


People also ask

Can we delete foreign key without deleting primary key?

To successfully change or delete a row in a foreign key constraint, you must first either delete the foreign key data in the foreign key table or change the foreign key data in the foreign key table, which links the foreign key to different primary key data.

How do I delete a foreign key in mysql?

Dropping Foreign Key Constraints You can drop a foreign key constraint using the following ALTER TABLE syntax: ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol; If the FOREIGN KEY clause defined a CONSTRAINT name when you created the constraint, you can refer to that name to drop the foreign key constraint.


1 Answers

# 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 

Offical doc: http://api.rubyonrails.org/v4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_foreign_key

like image 169
Dorian Avatar answered Sep 28 '22 04:09

Dorian