method

foreign_keys

Importance_0
v7.1.3.4 - Show latest stable - 0 notes - Class: AbstractMysqlAdapter
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 464
      def foreign_keys(table_name)
        raise ArgumentError unless table_name.present?

        scope = quoted_scope(table_name)

        # MySQL returns 1 row for each column of composite foreign keys.
        fk_info = internal_exec_query(<<~SQL, "SCHEMA")
          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',
                 fk.ordinal_position AS 'position',
                 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]}
        SQL

        grouped_fk = fk_info.group_by { |row| row["name"] }.values.each { |group| group.sort_by! { |row| row["position"] } }
        grouped_fk.map do |group|
          row = group.first
          options = {
            name: row["name"],
            on_update: extract_foreign_key_action(row["on_update"]),
            on_delete: extract_foreign_key_action(row["on_delete"])
          }

          if group.one?
            options[:column] = unquote_identifier(row["column"])
            options[:primary_key] = row["primary_key"]
          else
            options[:column] = group.map { |row| unquote_identifier(row["column"]) }
            options[:primary_key] = group.map { |row| row["primary_key"] }
          end

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