Can no longer pass an array of data to use inside `column IN ($1)` parameterized query
See original GitHub issueI apologize if there was an update that caused this and I missed it, but suddenly (using node-postgres version 6.1.5) the following query doesn’t work when it used to pretty recently:
import { Pool } from 'pg'
const pool = new Pool(config)
const { rows } = await pool.query('SELECT * FROM table WHERE id IN ($1)', [[1,2,3]]
I get the following error, which I haven’t gotten in the past:
error: invalid input syntax for integer: "{"1","2","3"}"
I also tried the following which isn’t working either:
const { rows } = await pool.query('SELECT * FROM table WHERE id IN ($1)', [[1,2,3].join(',')]
error: invalid input syntax for integer: "1,2,3"
How do I run a parameterized query using the IN
keyword and provide a list of data as we used to be able to before? Is there documentation on this breaking change somewhere I missed?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (1 by maintainers)
Oh, use
ANY
as @sehrope mentions above. So instead of usingcol IN (val1, val2, ...)
usecol = ANY($1)
with an array of values as your parameter for $1.AFAIK there isn’t any breaking change is as this is how both the server and node-postgres client have always been:
To do this type of thing replace the
value IN (...)
withvalue = ANY($1)
:Then the $1 can be an array. The
IN (...)
approach only works for a inline list in the SQL itself and requires dynamically composing (and escaping!) the values so they’re part of the SQL text. The= ANY($1)
approach is always better as it can be parameterized.