简体   繁体   中英

PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer

I am doing a "IN" query using prepared statements on rails. I am getting PG::InvalidTextRepresentation error.

code :

def mark_ineligible(match_ids)
  ids = match_ids.join(", ")
  result = epr("mark_matches_as_ineligible",
               "UPDATE matches SET is_eligibile=false WHERE id IN ( $1 )",
               [ids])
end

def epr(statementname, statement, params)
  connection = ActiveRecord::Base.connection.raw_connection
  begin
    result = connection.exec_prepared(statementname, params)
    return result
  rescue PG::InvalidSqlStatementName => e
    begin
      connection.prepare(statementname, statement)
    rescue PG::DuplicatePstatement => e
      # ignore since the prepared statement already exists
    end
    result = connection.exec_prepared(statementname, params)
    return result
  end
end

trying to invoke this using :

match_ids = [42, 43]
mark_ineligible match_ids
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "42, 43"

    from (irb):24:in `exec_prepared'
    from (irb):24:in `rescue in epr'
    from (irb):15:in `epr'
    from (irb):8:in `mark_ineligible'
    from (irb):35

Please help here. I want to know why I am getting this errors and how to fix it.

Thanks,

mark_ineligible should look as follows:

def mark_ineligible(match_ids)
    result = epr("mark_matches_as_ineligible",
           "UPDATE matches SET is_eligibile=false WHERE id IN ( $1 )", match_ids)
end

And when you call mark_ineligible , pass an array as argument:

mark_ineligible(match_ids)  #=>match_ids = [42,43]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM