sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Error: has incompatible types: sql.NullString, interface{}

Open omid9h opened this issue 1 year ago • 3 comments

I have a MySQL query with multiple named parameters. everything is fine but for bellow chunk:

AND (
    sqlc.narg(phone_filter) IS NULL
    OR (
        (
            length (sqlc.narg(phone_filter)) != 11
            OR aes_decrypt (from_base64 (u.phone), sqlc.arg(aes_key)) = sqlc.narg(phone_filter)
        )
        AND (
            length (sqlc.narg(phone_filter)) = 11
            OR aes_decrypt (from_base64 (u.phone), sqlc.arg(aes_key)) LIKE '%' || sqlc.narg(phone_filter) || '%'
        )
    )
)

and for the last line OR aes_decrypt (from_base64 (u.phone), sqlc.arg(aes_key)) LIKE '%' || sqlc.narg(phone_filter) || '%'

I get this error:

# package demorepo error generating code: named param PhoneFilter has incompatible types: sql.NullString, interface{}

any idea how to fix this? thanks

omid9h avatar Oct 15 '24 14:10 omid9h

Something that I've found is that sqlc assumes the parameter here at LIKE '%' || sqlc.narg(phone_filter) || '%' to be interface{} and apparently it conflicts with sql.NullString which is assumed in above lines.

I was able to solve the error simply by using another param like sqlc.narg(phone_filter2) but my question is how can define the type explicitly. I've tried ::<type> but I get syntax error.

omid9h avatar Oct 15 '24 19:10 omid9h

@omid9h have you found a solution on this other than using another named parameter? FYI ::<type> is only supported for PostgreSQL, I believe.

bingbeann avatar Oct 15 '25 04:10 bingbeann

@bingbeann nah. I just did the work around I mentioned and call it the day. and yes you're right my most use cases is on postgres and there is no problem for it.

omid9h avatar Oct 15 '25 10:10 omid9h