2

I am running into an error when trying to use parameters as part of my query.

Noteworthy parts of my schema are:

name_tokens tsvector

cards.rarity card_rarity which is a custom enum created from: create type card_rarity as enum ('C', 'T', 'R', 'S', 'M', 'L', 'F', 'P');

The troublesome query is:

SELECT cards.* FROM cards WHERE
(name_tokens @@ to_tsquery("$1"::text)) AND
(cards.rarity = "$2"::card_rarity)
      ORDER BY cards.id;

with input of:

[ 'absorb & in', 'L' ]

When I run this, the error states:

UnhandledPromiseRejectionWarning: error: column "$3" does not exist.

When I run this query without parameters it works fine, ie:

SELECT cards.* FROM cards WHERE
(name_tokens @@ to_tsquery('absorb & in') AND
(cards.rarity = 'L')
      ORDER BY cards.id;

I would like to be able to utilize parameters as throwing user strings into a query directly can be dangerous.

Any ideas on why I'm getting this error? I'm assuming it's some incorrect formatting or use of quotes when I shouldn't or something. Any help is appreciated :)

Edit: It was suggested that I tried:

SELECT cards.* FROM cards WHERE
(name_tokens @@ to_tsquery($1::text)) AND
(cards.rarity = $2::card_rarity)
ORDER BY cards.id;

But when I do this, I get the following error: UnhandledPromiseRejectionWarning: error: could not determine data type of parameter $1

1 Answer 1

1

PostgreSQL uses double quotes for identifiers (such as table and column names) so this:

SELECT cards.* FROM cards WHERE
(name_tokens @@ to_tsquery("$1"::text)) AND
(cards.rarity = "$2"::card_rarity)
      ORDER BY cards.id;

contains two quoted identifiers "$1" and "$2" but no numbered placeholders.

If you want to use numbered placeholders, drop the double quotes:

SELECT cards.* FROM cards WHERE
(name_tokens @@ to_tsquery($1::text)) AND
(cards.rarity = $2::card_rarity)
      ORDER BY cards.id;

I'm not sure where it is getting "$3" from though, I'd guess that the error is coming from a different query with a similar quoting error.

Sign up to request clarification or add additional context in comments.

9 Comments

When I try that I get the following error: UnhandledPromiseRejectionWarning: error: could not determine data type of parameter $1
Are you sure you're looking at the right query? The type casts generally take care of that sort of issue.
Yes. This the line I'm calling the query: await pool.query(searchQuery.getSearchQuery(), searchQuery.getInputs()); Where searchQuery.getSearchQuery() returns: SELECT cards.* FROM cards WHERE (name_tokens @@ to_tsquery($1::text)) AND (cards.rarity = $2::card_rarity) and the input returns: [ 'absorb & in', 'L' ] I'm using the pg package from npm for the pooly.query.
Okay! I figured it out! So my .getSearchQuery() was being called again. And unfortunately it's not a true 'getting' in that it alters the state (which I should fix) which was resulting in an incorrect string the 2+ times it was called.
Yes I believe so, it was adding additional inputs each time it was called. All fixed now! Hope D&D was fun!
|

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.