0

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)"
3
  • 1
    The subquery must be part of the query, not of your Node-code. Rewriting the VALUES-part to use a SELECT might be cleaner: INSERT INTO tbl (field1, field2) SELECT $1, id FROM another_table LIMIT 1;. This also fixes the other syntax error in your SQL, the comma right before , values(. Commented Oct 25, 2023 at 15:27
  • ok, what does the fill query look like inside node-postgres? @FrankHeikens Commented Oct 25, 2023 at 15:46
  • solved by pg.query(INSERT INTO tbl (field1, field2) values ($1, $2) , ["'a'", "SELECT $1, id FROM another_table LIMIT 1"]) note that the subquery is not wrapped by single quoyes Commented Oct 26, 2023 at 6:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.