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
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.