-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to pass an array as param? #2268
Comments
The driver will serialize JavaScript arrays as PostgreSQL arrays. So the first element of the "params" argument to const sql = 'INSERT sessions(all_programs) values($1) RETURNING *';
const params = [
['c6dc4514-8e7b-4043-9eb8-c7fd8ad2c306'],
];
const result = await db.query(sql, params); This example does not need casts as the server should be able to infer the column type but for other SQL you might need to add casts where you're using the parameters (the INSERT sessions(all_programs)
VALUES ($1::uuid[])
RETURNING * |
Hmmm....
results in Trace (see bellow). Doing the casted version yields the exact same trace.
server_1 | error: syntax error at or near "sessions"
|
|
I'd like to reopen this as this is exactly the issue I'm having with a simple :
I pass params like that :
where What I get in the postgres log (with all statements enabled) is kinda funny :
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 :
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. |
@AxelTerizaki The SQL syntax for const ids = ['foo', 'bar', 'baz'];
const sql = 'DELETE FROM table WHERE id = ANY($1::text[])';
const params = [ids];
client.query(sql, params); |
DELETE FROM table WHERE id = ANY ($1) |
@sehrope @charmander wow, thanks for the quick replies. I learned something today :) That worked! |
@sehrope @charmander @AxelTerizaki thank you. This helped me too :) |
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:
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 );
The text was updated successfully, but these errors were encountered: