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.

How to pass an array as param?

See original GitHub issue

Newbie to PG.

Passing the following Query works fine: 'INSERT sessions(all_programs) values(ARRAY[UUID(\'c6dc4514-8e7b-4043-9eb8-c7fd8ad2c306\')]) RETURNING *;'

Is there a way to pass the array of UUID as a param? The following very naive attempt:

const result = (await db.query(
     'INSERT sessions(all_programs) values($1) RETURNING *;'),
     ['ARRAY[UUID(\'c6dc4514-8e7b-4043-9eb8-c7fd8ad2c306\')]']);

Results in an err.

For completeness, the table schema is: CREATE TABLE sessions ( session_uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), all_programs UUID ARRAY );

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
sehropecommented, Apr 19, 2021

@AxelTerizaki The SQL syntax for foo IN (...) is for an inline values list. You can’t put a parameter in place of the list. Instead, use the array comparison construct ANY:

const ids = ['foo', 'bar', 'baz'];

const sql = 'DELETE FROM table WHERE id = ANY($1::text[])';
const params = [ids];
client.query(sql, params);
5reactions
AxelTerizakicommented, Apr 19, 2021

I’d like to reopen this as this is exactly the issue I’m having with a simple :

DELETE FROM table WHERE id IN $1

I pass params like that :

query(myQuery, [ids])

where ids is an array of strings.

What I get in the postgres log (with all statements enabled) is kinda funny :

2021-04-19 18:53:02.304 CEST [16576] ERREUR:  erreur de syntaxe sur ou près de « $1 » au caractère 35
2021-04-19 18:53:02.304 CEST [16576] INSTRUCTION :  
	DELETE FROM kara WHERE pk_kid IN $1;

Sorry the log is in french but it’s basically “syntax error on or near $1 at character 35”

It’s almost as if the query wasn’t being parameterized

It works if instead of passing $1 I manually do something dirty like this :

const kidList = JSON.stringify(kids).replace('[','(').replace(']',')').replace(/"/g, '\'');

I’m using node-postgres 8.6.0.

The snippet I posted above is working for me as a workaround, but it’s kind of messy and I wonder why this isn’t working even though it should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing Arrays as Function Arguments in C - Tutorialspoint
If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in...
Read more >
Pass arrays to a function in C - Programiz
To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num);. However,...
Read more >
How to pass an array within a query string? - Stack Overflow
Here's what I figured out: Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ...
Read more >
Passing arrays as arguments - C# Programming Guide
Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements....
Read more >
How Arrays are Passed to Functions in C/C++? - GeeksforGeeks
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array...
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