question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can no longer pass an array of data to use inside `column IN ($1)` parameterized query

See original GitHub issue

I apologize if there was an update that caused this and I missed it, but suddenly (using node-postgres version 6.1.5) the following query doesn’t work when it used to pretty recently:

import { Pool } from 'pg' const pool = new Pool(config) const { rows } = await pool.query('SELECT * FROM table WHERE id IN ($1)', [[1,2,3]]

I get the following error, which I haven’t gotten in the past:

error: invalid input syntax for integer: "{"1","2","3"}"

I also tried the following which isn’t working either:

const { rows } = await pool.query('SELECT * FROM table WHERE id IN ($1)', [[1,2,3].join(',')] error: invalid input syntax for integer: "1,2,3"

How do I run a parameterized query using the IN keyword and provide a list of data as we used to be able to before? Is there documentation on this breaking change somewhere I missed?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
whatl3ycommented, Mar 12, 2019

Oh, use ANY as @sehrope mentions above. So instead of using col IN (val1, val2, ...) use col = ANY($1) with an array of values as your parameter for $1.

const arrayOfStrings = ['val1', 'val2', 'val3']
const { rows } = await pool.query('SELECT * FROM table WHERE my_string_column = ANY($1)', [arrayOfStrings])
1reaction
sehropecommented, May 21, 2018

AFAIK there isn’t any breaking change is as this is how both the server and node-postgres client have always been:

=> SELECT 1 WHERE 1 IN ('{1,2,3}'::int[]);
ERROR:  42883: operator does not exist: integer = integer[]
LINE 1: SELECT 1 WHERE 1 IN ('{1,2,3}'::int[]);
                         ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
LOCATION:  op_error, parse_oper.c:722

To do this type of thing replace the value IN (...) with value = ANY($1):

=> SELECT 1
    WHERE 1 = ANY('{1,2,3}'::int[]);
 ?column? 
----------
        1

Then the $1 can be an array. The IN (...) approach only works for a inline list in the SQL itself and requires dynamically composing (and escaping!) the values so they’re part of the SQL text. The = ANY($1) approach is always better as it can be parameterized.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing an array to a query using a WHERE clause
We can build an expression to place inside the () from our array. Note that there must be at least one value inside...
Read more >
Parameterize SQL queries - Learn | Hex Technologies
When you pass a variable with more than one value (e.g. a list or array) to a SQL query you use the same...
Read more >
Working with parameters in the sp_executesql stored procedure
This article will show how to use the sp_executesql system stored procedure to run static and dynamic SQL queries.
Read more >
Running parameterized queries | BigQuery - Google Cloud
To use an array type in a query parameter set the type to ARRAY<T> where T is the type of the elements in...
Read more >
Working With BigQuery Parameterized Queries: Made Easy 101
To use array type for Google BigQuery Parameterized Queries, you will have to set the type to ARRAY<T>. T is defined as the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found