我正在style text[]
使用 contains 运算符查询数组列 () @>
。我的原始查询是:
SELECT * FROM songs WHERE style @> '{Acoustic}'
当我将它放入节点(使用pg-pool)并尝试对其进行参数化时,值引用周围的大括号似乎阻止它正常工作。请注意以下示例:
const style = 'Acoustic';
// this works, but I don't want to in-line like this:
const { rows } = await pool.query(`SELECT * FROM songs WHERE style @> '{` + style + `}'`);
// this fails with 'error: bind message supplies 1 parameters, but prepared statement "" requires 0'
const { rows } = await pool.query(`SELECT * FROM songs WHERE style @> '{$1}'`, [style]);
参数化这个的正确方法是什么?