2016-05-06 128 views
2

我正在做一個使用準備語句在軌道上的「IN」查詢。我得到PG :: InvalidTextRepresentation錯誤。PG :: InvalidTextRepresentation:錯誤:整數無效的輸入語法

代碼:

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 

試圖利用這個調用:

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 

請在這裏幫助。我想知道爲什麼我得到這個錯誤以及如何解決它。

感謝,

+0

不是傳遞'[IDS]'的,嘗試傳遞'ids',因爲它是一個數組。 – dp7

+0

嗨,「ids」是逗號分隔的字符串。我期待最後的查詢看起來像「更新匹配SET is_eligibile = false在哪裏(42,43)」 – anshul410

+0

@ ansul410'ids = match_ids.join(「,」)' - 只需從您的'mark_ineligible'方法中刪除此和直接將'match_ids'傳遞給你的查詢。 – dp7

回答

0

mark_ineligible應該如下:

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

當你罵mark_ineligible,傳遞一個數組作爲參數:

mark_ineligible(match_ids) #=>match_ids = [42,43] 
相關問題