sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

NamedQuery error when use PostgreSQL type cast ::int

Open merlindeep opened this issue 7 years ago • 4 comments

When I use NamedQuery for PostgreSQL and use type cast ::int in query:

SELECT (d.categories->>0)::int AS category_id
FROM some_table d
WHERE id =:id

I get error:

error="pq: syntax error at or near \":\""

if I use CAST

SELECT CAST(d.categories->>0 AS integer) AS category_id
FROM some_table d
WHERE id =:id

query works fine

merlindeep avatar Jul 09 '18 15:07 merlindeep

Just spotted this but I'm guessing it's because you're using a NamedStmt which requires you to double escape all :. Your generated query will only be providing one : to PostgreSQL and then failing.

Try changing to:

SELECT (d.categories->>0)::::int AS category_id

kisamoto avatar Jul 15 '19 20:07 kisamoto

@kisamoto Thanks. Is this way to double-escape : documented?

powerman avatar Jul 27 '19 20:07 powerman

@powerman I didn't find it in the docs, instead I noticed it searching for a similar problem that led me to #91

kisamoto avatar Jul 28 '19 08:07 kisamoto

@kisamoto Try changing to:

SELECT (d.categories->>0)::::int AS category_id

image

quenbyako avatar Sep 22 '23 02:09 quenbyako