简体   繁体   中英

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.

It's working only on straight INSERT INTO queries.


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 ,因此引擎甚至无法找到查询字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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