using a subquery as a value in node-postgres

53 Views Asked by At

I need to perform something like this:

pg.query(`insert into tbl (field1, field2) values($1, $2)`, ["a", "(select id from another_table limit 1)"])

this causes the following error

error: invalid input syntax for type uuid: "(select id from another_table limit 1)"
1

There are 1 best solutions below

0
Frank Heikens On

Rewrite the INSERT to use a SELECT:

pg.query(`INSERT INTO tbl (field1, field2) SELECT $1, id FROM another_table LIMIT 1;`, ["a"])

However, a LIMIT without an ORDER BY is rather strange and can lead to unexpected results.