"Operator does not exist: integer =?" when using Postgres
I have a simple SQL query called in the QueryRow method provided by the go / sql package.
import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/render"
"net/http"
"database/sql"
"fmt"
_ "github.com/lib/pq")
)
type User struct {
Name string
}
func Show(db *sql.DB, params martini.Params) {
id := params["id"]
row := db.QueryRow(
"SELECT name FROM users WHERE id=?", id)
u := User{}
err := row.Scan(&u.Name)
fmt.Println(err)
}
However, I get the error pq: operator does not exist: integer =?
It looks like the code doesn't understand what ?
is just a placeholder. How can I fix this?
source to share
PostgreSQL works with numbered aggregates ( $1
, $2
, ...) instead of the original position of question marks. The documentation for the Go interface also uses numbered placeholders in their examples:
rows, err := db.Query("SELECT name FROM users WHERE age = $1", age)
It seems that the Go interface doesn't translate question marks to numbered placeholders the way many interfaces do so that the question mark reaches the database and confuses things.
You should be able to switch to numbered placeholders instead of question marks:
row := db.QueryRow(
"SELECT name FROM users WHERE id = $1", id)
source to share