提问人:Sherif eldeeb 提问时间:10/25/2023 最后编辑:Sherif eldeeb 更新时间:10/25/2023 访问量:20
在 node-postgres 中使用子查询作为值
using a subquery as a value in node-postgres
问:
我需要执行如下操作:
pg.query(`insert into tbl (field1, field2) values($1, $2)`, ["a", "(select id from another_table limit 1)"])
这会导致以下错误
error: invalid input syntax for type uuid: "(select id from another_table limit 1)"
答:
1赞
Frank Heikens
10/25/2023
#1
重写 INSERT 以使用 SELECT:
pg.query(`INSERT INTO tbl (field1, field2) SELECT $1, id FROM another_table LIMIT 1;`, ["a"])
但是,没有 ORDER BY 的 LIMIT 是相当奇怪的,可能会导致意想不到的结果。
评论
INSERT INTO tbl (field1, field2) SELECT $1, id FROM another_table LIMIT 1;
, values(
node-postgres
pg.query(
, ["'a'", "SELECT $1, id FROM another_table LIMIT 1"])