37

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.

Mel
  • 2,481
  • 26
  • 113
  • 273
  • Manually remove the last created migration which shows the error and juts rn `rake db:rollback` to revert to the second last migration. – Rajesh Omanakuttan May 18 '14 at 03:38
  • i have many migrations that were created since this migration. i tried to migrate:down using the timestamp reference but the command failed. – Mel May 18 '14 at 03:40
  • `foreign key does not exist` error may be because you might not have defined model relationships inside the models.. – Rajesh Omanakuttan May 18 '14 at 03:44
  • http://stackoverflow.com/questions/3380504/removing-foreign-key-using-matthuhiggins-foreign-key, this may also help you out.. – Rajesh Omanakuttan May 18 '14 at 03:45
  • Thanks. I tried uncommenting the lines in the rails migrations that create the mailboxer foreign keys as well as the id link between those tables, then I tried both resetting and migrating and then dropping, creating and migrating the database which now has those foreign key lines commented out. In each case, the schema did not update and the reference in it to there being a foreign key remains. Mailboxer did not make models for these objects - just migrations. It's really weird. But I still have this problem. – Mel May 18 '14 at 04:23

3 Answers3

52
# 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

Dorian
  • 22,759
  • 8
  • 120
  • 116
42

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"
cschroed
  • 6,304
  • 6
  • 42
  • 56
  • 1
    Thank you very much. I ended up dropping my DB and recreating it - after I had commented out the foreign key attributes in the mailboxer migrations. I won't be able to use this advice this time around but it looks like it is probably the trick - if others are having the same problem. Thank you. – Mel May 18 '14 at 21:48
0

When I ran that I got:

 NoMethodError: undefined method `remove_foreign_key' for #<ActiveRecord::ConnectionAdapters::Table:0x00007faa35e94aa8>
Did you mean?  remove_index

Words of wisdom- never use anything except an id integer for a foreign key. I used a title param while practicing on a fake app and it causes:

ActiveRecord::AssociationTypeMismatch (Company(#70210936585940) expected, got "Company4" which is an instance of String(#70210933923380))
  • it does not answer the question. If you want to point out a problem in another answer, comment below it please :) – ARK Dec 05 '19 at 07:55