instance method foreign_key_exists?

Ruby on Rails 7.2.3

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

foreign_key_exists?(from_table, to_table = nil, **options)

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")

Parameters

from_table req
to_table opt = nil
options keyrest
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1219
      def foreign_key_exists?(from_table, to_table = nil, **options)
        foreign_key_for(from_table, to_table: to_table, **options).present?
      end

Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 1219 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::SchemaStatements

Type at least 2 characters to search.

↑↓ navigate · open · esc close