跳至内容 跳至搜索

当外键约束无法添加,因为列类型与引用列类型不匹配时引发。

方法
N
S

类公共方法

new( message: nil, sql: nil, binds: nil, table: nil, foreign_key: nil, target_table: nil, primary_key: nil, primary_key_column: nil, query_parser: nil, connection_pool: nil )

# File activerecord/lib/active_record/errors.rb, line 239
    def initialize(
      message: nil,
      sql: nil,
      binds: nil,
      table: nil,
      foreign_key: nil,
      target_table: nil,
      primary_key: nil,
      primary_key_column: nil,
      query_parser: nil,
      connection_pool: nil
    )
      @original_message = message
      @query_parser = query_parser

      if table
        type = primary_key_column.bigint? ? :bigint : primary_key_column.type
        msg = <<~EOM.squish
          Column `#{foreign_key}` on table `#{table}` does not match column `#{primary_key}` on `#{target_table}`,
          which has type `#{primary_key_column.sql_type}`.
          To resolve this issue, change the type of the `#{foreign_key}` column on `#{table}` to be :#{type}.
          (For example `t.#{type} :#{foreign_key}`).
        EOM
      else
        msg = <<~EOM.squish
          There is a mismatch between the foreign key and primary key column types.
          Verify that the foreign key column type and the primary key of the associated table match types.
        EOM
      end
      if message
        msg << "\nOriginal message: #{message}"
      end

      super(msg, sql: sql, binds: binds, connection_pool: connection_pool)
    end

实例公共方法

set_query(sql, binds)

# File activerecord/lib/active_record/errors.rb, line 275
def set_query(sql, binds)
  if @query_parser && !@sql
    self.class.new(
      message: @original_message,
      sql: sql,
      binds: binds,
      connection_pool: @connection_pool,
      **@query_parser.call(sql)
    ).tap do |exception|
      exception.set_backtrace backtrace
    end
  else
    super
  end
end