method

foreign_keys

Importance_0
v5.1.7 - Show latest stable - 0 notes - Class: ActiveRecord::ConnectionAdapters
  • 1.0.0
  • 1.1.6
  • 1.2.6
  • 2.0.3
  • 2.1.0
  • 2.2.1
  • 2.3.8
  • 3.0.0
  • 3.0.9
  • 3.1.0
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.0.0.1 (0)
  • 5.1.7 (0)
  • 5.2.3 (0)
  • 6.0.0
  • 6.1.3.1
  • 6.1.7.7
  • 7.0.0
  • 7.1.3.2
  • 7.1.3.4
  • 7.2.3
  • 8.0.0
  • 8.1.1
  • What's this?
foreign_keys(table_name) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb, line 455
      def foreign_keys(table_name)
        raise ArgumentError unless table_name.present?

        scope = quoted_scope(table_name)

        fk_info = exec_query(          SELECT fk.referenced_table_name AS 'to_table',                 fk.referenced_column_name AS 'primary_key',                 fk.column_name AS 'column',                 fk.constraint_name AS 'name',                 rc.update_rule AS 'on_update',                 rc.delete_rule AS 'on_delete'          FROM information_schema.referential_constraints rc          JOIN information_schema.key_column_usage fk          USING (constraint_schema, constraint_name)          WHERE fk.referenced_column_name IS NOT NULL            AND fk.table_schema = #{scope[:schema]}            AND fk.table_name = #{scope[:name]}            AND rc.constraint_schema = #{scope[:schema]}            AND rc.table_name = #{scope[:name]}.strip_heredoc, "SCHEMA")

        fk_info.map do |row|
          options = {
            column: row["column"],
            name: row["name"],
            primary_key: row["primary_key"]
          }

          options[:on_update] = extract_foreign_key_action(row["on_update"])
          options[:on_delete] = extract_foreign_key_action(row["on_delete"])

          ForeignKeyDefinition.new(table_name, row["to_table"], options)
        end
      end
Register or log in to add new notes.