Ruby-on-rails 外国人 - 删除外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23718292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:38:33  来源:igfitidea点击:

foreigner - remove foreign key

ruby-on-railspsqlmailboxerforeigner

提问by Mel

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.

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

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 中止并说外键不存在。

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"

我的架构包括:add_foreign_key“notifications”,“conversations”,名称:“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'.

我试图将 db:migrate:down 原始迁移创建邮箱,但也收到一条错误消息,提示“未找到命令”。

Can anyone help? Thank you.

任何人都可以帮忙吗?谢谢你。

回答by cschroed

The add_foreign_keycommand 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_keycommand must specify the existing foreign key name instead of the column name. Try:

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

remove_foreign_key :notifications, name: "notifications_on_conversation_id"

回答by Dorian

# 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

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

回答by CodingBingo

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:

智慧之言 - 除了外键的 id 整数外,永远不要使用任何东西。我在一个假的应用程序上练习时使用了一个标题参数,它导致:

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