我试图一次更新多行。 为此,我正在制作以下准备好的陈述:
WITH new_values (id, data, email) AS (
VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9), ($10, $11, $12)
)
UPDATE prospects
SET
data = new_values.data,
email = new_values.email
FROM new_values
WHERE
prospects.id = new_values.id
RETURNING *
但我收到以下错误:
error: bind message supplies 12 parameters, but prepared statement "prospects-multi-update" requires 0
api-prospects_1 | at Connection.parseE (/usr/src/app/node_modules/pg/lib/connection.js:546:11)
api-prospects_1 | at Connection.parseMessage (/usr/src/app/node_modules/pg/lib/connection.js:371:19)
api-prospects_1 | at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:114:22)
api-prospects_1 | at Socket.emit (events.js:180:13)
api-prospects_1 | at addChunk (_stream_readable.js:274:12)
api-prospects_1 | at readableAddChunk (_stream_readable.js:261:11)
api-prospects_1 | at Socket.Readable.push (_stream_readable.js:218:10)
api-prospects_1 | at TCP.onread (net.js:581:20)
我尝试了不同的查询(使用INSERT ... ON CONFLICT DO UPDATE
),但它也是如此。
它仅适用于直接INSERT INTO
次查询。
以下是调用此查询的代码: (我删除了字符串生成函数,因为它不是很有用)
const upsertValues = async (entities) => {
if (entities.length === 0) return []
const client = pool.connect()
const values = [...]
const variables = [...]
const setStatements = [...]
const res = await client.query({
name: `${resourceName}-multi-update`,
test: `
WITH new_values (id, ${columns}) AS (
VALUES ${variables}
)
UPDATE prospects
SET ${setStatements}
FROM new_values
WHERE
prospects.id = new_values.id
RETURNING *
`,
values: flatten(values),
})
return res.row || []
}
答案 0 :(得分:1)
错误是由于准备好的语句的属性text
被错误地命名为test
,因此引擎甚至无法找到查询字符串。