简体   繁体   English

预处理语句无法识别Postgres JS绑定上的任何参数

[英]Prepared Statement doesn't recognize any parameters on Postgres JS binding

I'm trying to make an update for multiple rows at once. 我正在尝试一次更新多个行。 For this, I'm making the folowing prepared statement : 为此,我正在准备以下声明:

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 *

But I'm getting the following error : 但是我收到以下错误:

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)

I have tried different queries (with INSERT ... ON CONFLICT DO UPDATE ) but it does the same. 我尝试了不同的查询(使用INSERT ... ON CONFLICT DO UPDATE ),但是它执行相同的操作。

It's working only on straight INSERT INTO queries. 它仅适用于直接INSERT INTO查询。


EDIT 编辑

Here is the code calling this query : (I have remove the string generation functions since it's not very useful) 这是调用此查询的代码:(我删除了字符串生成函数,因为它不是很有用)

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 || []
}

该错误是由于已准备好的语句的属性text被误命名为test ,因此引擎甚至无法找到查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM