This is explained in Query Formatting, how one value versus array changes the values interpretation:
This however works only for types number
, bigint
, string
, boolean
, Date
and null
, because types like Array
and Object
change the way parameters are interpreted.
db.any(`SELECT * FROM users WHERE user_id IN ($1:csv)`, team.member_ids)
//=> SELECT * FROM users WHERE user_id IN (1)
Because above you are passing in the value-array itself directly, $1
refers to the first element in that array. To make $1
refer to the entire value-array, that value itself needs to be inside an array:
db.any(`SELECT * FROM users WHERE user_id IN ($1:csv)`, [team.member_ids])
//=> SELECT * FROM users WHERE user_id IN (1,2)
Alternatively, the recommended use of Named Parameters makes the code more concise:
db.any(`SELECT * FROM users WHERE user_id IN ($<member_ids:csv>)`, team)
//=> SELECT * FROM users WHERE user_id IN (1,2)
In general, use pg-monitor to see what queries are being executed.