0

I am running the below SQL and it works fine:

  const ids = book.map(({ id }) => id);
  const myQuery = `
    SELECT
      row_to_json(s)
    FROM (
      SELECT
        b.id, b.event_id, b.title, b.price, e.account_id
      FROM
        ticket_books b, event e
      WHERE
        b.event_id = e.id AND
        b.id = ANY(ARRAY[${ids}])
    ) s
  `;
  const result = await server.pg.query(myQuery);

I would like to switch to prepared statements, so I rewrote the above as:

  const myQuery = `
    SELECT
      row_to_json(s)
    FROM (
      SELECT
        b.id, b.event_id, b.title, b.price, e.account_id
      FROM
        ticket_books b, event e
      WHERE
        b.event_id = e.id AND
        b.id = ANY(ARRAY[$1])
    ) s
  `;
  const result = await server.pg.query(myQuery, [ids]);

The prepared statement version is failing with: error: operator does not exist: integer = text.

What is the problem?

7
  • What is in ids? Is it an array of integers or strings? Commented Aug 4, 2021 at 18:27
  • a bunch of integers. it is constructed at the beginning of the code. Commented Aug 4, 2021 at 18:29
  • Are you sure they're integers and not strings? Commented Aug 4, 2021 at 18:29
  • If you hard code const ids = [1,2,3] does it work? Commented Aug 4, 2021 at 18:30
  • i am pretty sure ids are of type number. Commented Aug 4, 2021 at 18:33

1 Answer 1

2

pg will make the array for you. Use b.id = ANY($1).

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.