title: How do I drop a foreign key if it exists in Ruby on Rails?
body: <p>There's a function called <code>index_exists?</code> in ActionRecord, but no <code>foreign_key_exists?</code> on Rails 4.2.7.  </p>
  
  <p>So when I call <code>remove_foreign_key :parties, :franchise_groups</code> on some databases it breaks.</p>
  
  <p><strong>What should I use?</strong></p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>My code</p>
  
  <pre><code>class RemoveForeignKey &lt; ActiveRecord::Migration
    def up
      if foreign_key_exists?(:parties, :franchise_groups)
        remove_foreign_key :parties, :franchise_groups
      end
    end
  end
  </code></pre>
  
  <p>gives the error</p>
  
  <pre><code>== 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 #&lt;RemoveForeignKey:0x00000007ea0b58&gt;
  /home/rje/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7/lib/active_record/migration.rb:664:in `block in method_missing'
  </code></pre>