def foreign_keys(table_name)
      table_name = table_name.to_s

      @foreign_keys ||= {}
      @foreign_keys[table_name] ||= begin
        if @registration[:foreign_keys].present?
          @registration[:foreign_keys].call(@connection, table_name)
        else
          begin
            @connection.foreign_keys(table_name)
          rescue ::NoMethodError, ::NotImplementedError
            ## Not all ActiveRecord adapters support `#foreign_keys`. When this happens, we'll make
            ## a best-effort attempt to intuit relationships from the table and column names.
            ##
            columns(table_name).map do |column|
              id_pattern = %r{(?:_id|Id)$}
              next unless column.name =~ id_pattern

              target_table = column.name.sub(id_pattern, '')
              next unless target_table.in? tables

              target_column = primary_key(qualified_name(target_table, context: table_name))
              next unless target_column.in? ['id', 'Id', 'ID', column.name]

              ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
                table_name.split('.').last,
                target_table,
                name: "FK_#{SecureRandom.uuid.delete('-')}",
                column:  column.name,
                primary_key: target_column,
                on_update: nil,
                on_delete: nil
              )
            end.compact
          end
        end
      end
    end
def foreign_keys(table_name)
      table_name = table_name.to_s

      @foreign_keys ||= {}
      @foreign_keys[table_name] ||= begin
        if @registration[:foreign_keys].present?
          @registration[:foreign_keys].call(@connection, table_name)
        else
          begin
            @connection.foreign_keys(table_name)
          rescue ::NoMethodError, ::NotImplementedError
            ## Not all ActiveRecord adapters support `#foreign_keys`. When this happens, we'll make
            ## a best-effort attempt to intuit relationships from the table and column names.
            ##
            columns(table_name).map do |column|
              id_pattern = %r{(?:_id|Id)$}
              next unless column.name =~ id_pattern

              target_table = column.name.sub(id_pattern, '')
              next unless target_table.in? tables

              target_column = primary_key(qualified_name(target_table, context: table_name))
              next unless target_column.in? ['id', 'Id', 'ID', column.name]

              ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
                table_name.split('.').last,
                target_table,
                name: "FK_#{SecureRandom.uuid.delete('-')}",
                column:  column.name,
                primary_key: target_column,
                on_update: nil,
                on_delete: nil
              )
            end.compact
          end
        end
      end
    end
Returns a list of foreign keys for the given table.

 If a matching Automodel::SchemaInspector registration is found for the connection's adapter,
 and that registration specified a `:foreign_keys` Proc, the Proc is called. Otherwise, the
 standard connection `#foreign_keys` is attempted. If that call to ``#foreign_keys` raises a
 ::NoMethodError or ::NotImplementedError, a best-effort attempt is made to build a list of
 foreign keys based on table and column names.


 @param table_name [String]
   The table whose foreign keys should be fetched.


 @return [Array<ActiveRecord::ConnectionAdapters::ForeignKeyDefinition>]