使用 IN 子句有条件查询 Postgres 时出现协议冲突错误 08P01
当我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 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
所以上面的情况可以通过以下方式修复:
Found the solution on Github Q&A which shows the proper way to use the
IN
clausehttps://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:
就我而言,是引号引起的。
在整个语句周围使用双引号,在值周围使用单引号是有效的:
这会导致错误:
In my case, the quotes caused it.
Having double quotes around the whole statement and single quotes around values worked:
And this resulted in an error: