简体   繁体   中英

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

There's a function called index_exists? in ActionRecord, but no foreign_key_exists? on Rails 4.2.7.

So when I call remove_foreign_key:parties, :franchise_groups on some databases it breaks.

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?

There is 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 :

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 4.2.6), so I'm using Array#any? to search through the results from "foreign_keys" and determine whether a given foreign key exists:

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 adds if_exists option to remove_foreign_key in order to not raise an error when the foreign key is already removed.

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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