Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres list parameter in Go (using database/sql and pq)

I'm trying to write a query that takes a list parameter (ie, a single parameter which is a list of values). It appears that this is at least sometimes possible in PostgreSQL (https://stackoverflow.com/a/10829760/836390). What I want is something like this:

rows, err := db.Query("SELECT * FROM table WHERE id in $1", []int{1, 2, 3})

However, when I execute this using the pq driver, I get an error:

sql: converting Exec argument #0's type: unsupported type []int, a slice

Is this simply not supported in pq yet, or is this not supported in database/sql, or not in PostgreSQL at all, or what?

like image 933
joshlf Avatar asked Dec 11 '22 08:12

joshlf


1 Answers

You can use pq.Array with slice parameters nowadays. So the query would look like:

rows, err := db.Query("SELECT * FROM table WHERE id in $1", pq.Array([]int{1, 2, 3}))
like image 74
hullarb Avatar answered Jan 15 '23 15:01

hullarb