Can't compare strings when COLLATE'ing (But I can when using pgx/v5??) #117849
-
I have the following table structure: CREATE TABLE IF NOT EXISTS "user"
(
id BIGSERIAL PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE COLLATE "en-US-u-ks-level2",
password TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
); I'm trying to run the following query: SELECT created_at FROM "user" WHERE username = 'John'; However, I get the following error: And in my SQL editor I get the error: However, the wackiest of all, in my Golang project I can perform this query fine against the same exactly database. I'm using SQLC with the pgx/v5 package. I'm quite stumped on this, if anyone has any ideas/suggestions I'd appreciate them. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
In short, what happens with the problematic statement is that untyped string My guess is that sqlc automatically adds the casts for you, so the statement that is actually executed is not SELECT created_at FROM "user" WHERE username = 'John'; but is something like: SELECT created_at FROM "user" WHERE username = 'John'::VARCHAR(32) COLLATE "en-US-u-ks-level2"; which works. |
Beta Was this translation helpful? Give feedback.
In short, what happens with the problematic statement is that untyped string
'John'
is treated by CRDB asSTRING
type, and comparing a collated string against a "regular" string isn't supported, so you must help the type checker a bit (e.g. by adding an explicit cast).My guess is that sqlc automatically adds the casts for you, so the statement that is actually executed is not
but is something like:
which works.