“Operator does not exist: integer =?” when using Postgres

后端 未结 1 402
情话喂你
情话喂你 2020-11-27 07:58

I have a simple SQL query called within the QueryRow method provided by go\'s database/sql package.

import (
  \"github.com/codegangsta/martini\"
  \"githu         


        
相关标签:
1条回答
  • 2020-11-27 09:01

    PostgreSQL works with numbered placeholders ($1, $2, ...) natively rather than the usual positional question marks. The documentation for the Go interface also uses numbered placeholders in its examples:

    rows, err := db.Query("SELECT name FROM users WHERE age = $1", age)
    

    Seems that the Go interface isn't translating the question marks to numbered placeholders the way many interfaces do so the question mark is getting all the way to the database and confusing everything.

    You should be able to switch to numbered placeholders instead of question marks:

     row := db.QueryRow(
        "SELECT name FROM users WHERE id = $1", id)
    
    0 讨论(0)
提交回复
热议问题