使用 IN 子句有条件查询 Postgres 时出现协议冲突错误 08P01

发布于 2025-01-15 20:14:40 字数 811 浏览 1 评论 0原文

当我尝试使用 IN 子句选择多行数据时,我不断收到 Postgres 错误 08P01。

下面是我的代码片段...

stripeRouter.post("/create-payment-intent", async (req, res, next) => {
  try {
    const order = req.body as { product_uuid: string; quantity: number }[];
    const allProductIds = order.map((item) => item.product_uuid);
    const productsInDb = await pool.query(
      "SELECT product_uuid, price FROM product WHERE product_uuid IN ($1) AND inventory > 0",
      allProductIds
    );
    ...

查找代码 ID,这似乎是某种连接错误。如果 allProductIds 仅包含 1 个项目,错误就会消失。

这是我的 Express 路由器中从节点 pg 抛出的错误

"original": {
   "length": 137,
   "name": "error",
   "severity": "ERROR",
   "code": "08P01",
   "file": "postgres.c",
   "line": "1675",
   "routine": "exec_bind_message"
}

I keep getting Postgres error 08P01 when I try to select multiple rows of data with the IN clause.

Below is a snippet of my code ...

stripeRouter.post("/create-payment-intent", async (req, res, next) => {
  try {
    const order = req.body as { product_uuid: string; quantity: number }[];
    const allProductIds = order.map((item) => item.product_uuid);
    const productsInDb = await pool.query(
      "SELECT product_uuid, price FROM product WHERE product_uuid IN ($1) AND inventory > 0",
      allProductIds
    );
    ...

Looking up the code id it seems this is some sort of connection error. And the error goes away if allProductIds contains only 1 item.

Here's the error thrown in my express router from node pg

"original": {
   "length": 137,
   "name": "error",
   "severity": "ERROR",
   "code": "08P01",
   "file": "postgres.c",
   "line": "1675",
   "routine": "exec_bind_message"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2025-01-22 20:14:40

在 Github Q&A 上找到了解决方案,其中显示了使用 IN 子句的正确方法
https://github.com/brianc/node-postgres/wiki/FAQ#11-how-do-i-build-a-where-foo-in--query-to-find-rows-matching- an-array-of-values

所以上面的情况可以通过以下方式修复:

const order = req.body as { product_uuid: string; quantity: number }[];
    const allProductIds = order.map((item, index) => item.product_uuid);

    const productsInDb = await pool.query(
      `SELECT product_uuid, price FROM product WHERE product_uuid IN (${allProductIds
        .map((id, i) => `${i + 1}`)
        .join(",")})`,
      allProductIds
    );
...

Found the solution on Github Q&A which shows the proper way to use the IN clause
https://github.com/brianc/node-postgres/wiki/FAQ#11-how-do-i-build-a-where-foo-in--query-to-find-rows-matching-an-array-of-values

So the above case can be fixed in the following way:

const order = req.body as { product_uuid: string; quantity: number }[];
    const allProductIds = order.map((item, index) => item.product_uuid);

    const productsInDb = await pool.query(
      `SELECT product_uuid, price FROM product WHERE product_uuid IN (${allProductIds
        .map((id, i) => `${i + 1}`)
        .join(",")})`,
      allProductIds
    );
...
预谋 2025-01-22 20:14:40

就我而言,是引号引起的。

在整个语句周围使用双引号,在值周围使用单引号是有效的:

await client.query("INSERT INTO companies (social_security_number) VALUES ('test')")

这会导致错误:

await client.query('INSERT INTO companies (social_security_number) VALUES ("test")')

In my case, the quotes caused it.

Having double quotes around the whole statement and single quotes around values worked:

await client.query("INSERT INTO companies (social_security_number) VALUES ('test')")

And this resulted in an error:

await client.query('INSERT INTO companies (social_security_number) VALUES ("test")')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文