Skip to content
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

Search an integer array fails, invalid syntax for integer #2495

Closed
dl-husky73 opened this issue Mar 16, 2021 · 4 comments
Closed

Search an integer array fails, invalid syntax for integer #2495

dl-husky73 opened this issue Mar 16, 2021 · 4 comments
Labels

Comments

@dl-husky73
Copy link

I am referencing JSON / Array issue #442

RN - 65
Node - 14
Express - 4.1

I have a very simple PG Query that works fine in Postgre:
SELECT * from table1 where field1 @> '{619327}'
Field1 is an Integer Array with intarray extension, index.

In PG Tried Several Iterations with no success ->
const url = "SELECT * from table1 where field1 @> '{$1}' Error: invalid input syntax for type integer: "$1" at character 45
const url = "SELECT * from table1 where field1 @> '${1}'::json
const url = "SELECT * from table1 where field1 @> '$1'::json
const url = "SELECT * from table1 where field1 @> '{$1}'::jsonb
const url = "SELECT * from table1 where field1 @> '$1'::jsonb Error:Token Invalid. :CONTEXT: JSON data, line 1: $...
return client.query(url, addValues)

I saw #442 and tried a few things. I am not exactly sure what to do. Anyone solve this problem for an Integer?

Struggling.

@charmander
Copy link
Collaborator

SELECT * FROM table1 WHERE field1 @> $1

@dl-husky73
Copy link
Author

Thanks for the quick reply! I know the Intarray extension on an Integer multi-array requires to use {}. I would have felt pretty silly if it was a straight forward application.

I changed to above and got the following errors:
2021-03-16 01:49:23[19492]:ERROR: malformed array literal: "619327"
2021-03-16 01:49:23 [19492]:DETAIL: Array value must start with "{" or dimension information.
2021-03-16 01:49:23 [19492]:STATEMENT: SELECT * from table1 where field1 @> $1 and guid=$2

@charmander
Copy link
Collaborator

charmander commented Mar 16, 2021

When you pass the parameter, it needs to be as an array (or the string form of an array). So if you had:

client.query("SELECT * FROM table1 WHERE field1 @> $1 AND guid = $2", [619327, someGuid])

the version with a correct parameter value would be this:

client.query("SELECT * FROM table1 WHERE field1 @> $1 AND guid = $2", [[619327], someGuid])

Or, if the parameter is always one element, you can make the array in the query:

client.query("SELECT * FROM table1 WHERE field1 @> ARRAY[$1] AND guid = $2", [619327, someGuid])

@dl-husky73
Copy link
Author

The first option worked, thanks for the feedback!
client.query("SELECT * FROM table1 WHERE field1 @> $1 AND guid = $2", [[619327], someGuid]) worked which
showed up as '{619327}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants