外键-移除外键



我试图在我的rails 4应用程序中使用mailboxer。当我尝试部署db时出现问题。在创建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

我的schema包括:Add_foreign_key "notifications", "conversations", name: "notifications_on_conversation_id"

我试着把db:migrate:down原来创建mailboxer的迁移,但是也得到了一个错误说'command not found'。

有人能帮忙吗?谢谢你。

# 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

模式中的add_foreign_key命令将外键命名为notifications_on_conversation_id。这个名称不同于外国人通常根据列名分配的默认名称notifications_conversation_id_fk。因此,remove_foreign_key命令必须指定现有的外键名,而不是列名。试一试:

remove_foreign_key :notifications, name: "notifications_on_conversation_id"

当我运行时,我得到:

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

至理名言-永远不要使用除了id整数以外的任何东西作为外键。我在一个假应用上练习时使用了一个title参数,它会导致:

ActiveRecord::AssociationTypeMismatch (Company(#70210936585940) expected, got "Company4" which is an instance of String(#70210933923380))

相关内容

  • 没有找到相关文章

最新更新