简体   繁体   English

如果外键存在于 Ruby on Rails 中,我该如何删除它?

[英]How do I drop a foreign key if it exists in Ruby on Rails?

There's a function called index_exists?有一个叫index_exists? in ActionRecord, but no foreign_key_exists?在 ActionRecord 中,但没有foreign_key_exists? on Rails 4.2.7.在 Rails 4.2.7 上。

So when I call remove_foreign_key:parties, :franchise_groups on some databases it breaks.因此,当我在某些数据库上调用remove_foreign_key:parties, :franchise_groups时,它会中断。

What should I use?我应该使用什么?


Update更新

My code我的代码

class RemoveForeignKey < ActiveRecord::Migration
  def up
    if foreign_key_exists?(:parties, :franchise_groups)
      remove_foreign_key :parties, :franchise_groups
    end
  end
end

gives the error给出错误

== 20161107163800 RemoveForeignKey: migrating =================================
-- foreign_key_exists?(:parties, :franchise_groups)
rake aborted!
An error has occurred, all later migrations canceled:

undefined method `foreign_key_exists?' for #<RemoveForeignKey:0x00000007ea0b58>
/home/rje/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7/lib/active_record/migration.rb:664:in `block in method_missing'

but no foreign_key_exists? 但没有foreign_key_exists?

There is foreign_key_exists? foreign_key_exists? :) :)

Checks to see if a foreign key exists on a table for a given foreign key definition. 检查表中是否存在用于给定外键定义的外键。

 # Checks to see if a foreign key exists. foreign_key_exists?(:accounts, :branches) 

# Checks to see if a foreign key on a specified column exists. foreign_key_exists?(:accounts, column: :owner_id)

# Checks to see if a foreign key with a custom name exists. foreign_key_exists?(:accounts, name: "special_fk_name")

Alternatively, you can use foreign_keys : 或者,您可以使用foreign_keys

if foreign_keys(:table_name).include?(foreign_key_name)
  # do stuff
end

在Rails 4上没有foreign_key_exists所以我提出了以下解决方案:

remove_foreign_key :events, column: :subscribers_selector_id if foreign_keys(:events).map(&:column).include?("subscribers_selector_id")

I think you can use something like this 我想你可以使用这样的东西

def up
  remove_foreign_key :parties, column: :franchise_groups
end

def down
  add_foreign_key :parties, :franchise_groups
end

它适用于连接:

ActiveRecord::Base.connection.foreign_key_exists?(:parties, :franchise_groups) 

My Rails version doesn't seem to have "foreign_key_exists?" 我的Rails版本似乎没有“foreign_key_exists?” (Rails 4.2.6), so I'm using Array#any? (Rails 4.2.6),所以我使用的是Array#any? to search through the results from "foreign_keys" and determine whether a given foreign key exists: 搜索“foreign_keys”的结果并确定是否存在给定的外键:

foreign_keys("parties").any?{|k| k[:to_table] == "franchise_groups"}

You can use it as such: 您可以这样使用它:

if foreign_keys("parties").any?{|k| k[:to_table] == "franchise_groups"}
remove_foreign_key :parties, column: :franchise_group_id
end

Rails 7+ if_exists / if_not_exists options Rails 7+ if_exists / if_not_exists 选项

Rails 7 adds if_exists option to remove_foreign_key in order to not raise an error when the foreign key is already removed. Rails 7 向remove_foreign_key添加了if_exists选项,以便在外键已被删除时不会引发错误。

Rails 7 adds if_not_exists option to add_foreign_key in order to not raise an error when the foreign key is already added. Rails 7 向add_foreign_key添加了if_not_exists选项,以便在已添加外键时不引发错误。

As a result, a migration can be written in the following way:因此,迁移可以按以下方式编写:

class RemoveFranchiseGroupForeignKeysFromParties < ActiveRecord::Migration
  def up
    remove_foreign_key :parties, :franchise_groups, if_exists: true
  end

  def down
    add_foreign_key :parties, :franchise_groups, if_not_exists: true
  end
end

Sources:资料来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM