Raw sql`` quoting issue

AAlcaponeYou4/17/2023
Hi, I doing a work around until Drizzle supports generated columns. I have a manual script that adds the generated column + its index.

I'm dynamically building the sql query since there's no typings yet.

const column = 'col-name'
sql`${column} < ${'10'}`


The syntax above returns no data or sometimes all the data b/c it generates: 'col-name' < '10', which doesn't match b/c of the quote around the column name.
If I hardcoded the query to:
sql`col-name < '10'`

Then it returns the correct results.

Am I using sql incorrectly?

Thanks!

NOTE: I'm doing ${'10'} to test the ${} syntax which seems to be working fine. It's only an issue w/ the column.
AAlcaponeYou4/17/2023
I got around this by using
new SQL([new StringChunk(`${someColumn} < 10`)])
Lluxaritas4/17/2023
What you're seeing here is expected. SQL does not "understand" left side vs right side of expressions. Whenever you interpolate, it always escapes the value you put in as if it's a value
Lluxaritas4/17/2023
Typically you want to refer to the column name via the schema you created
Lluxaritas4/17/2023
Eg, if you have a users table with a age column,
sql`${user.age} < ${'10'}
Lluxaritas4/17/2023
age in this case won't be a raw string, it'll be a data type the sanitizer knows it can inject "raw"
Bbloberenober4/17/2023
asuming someColumn is a string, you can do either sql.raw(`'${someColumn}'`) or name(someColumn)
Bbloberenober4/17/2023
(2nd one is preferred)
AAlcaponeYou4/17/2023
Thanks Luxaritas, I'm hacking about w/ generated columns so there's no typings yet.
AAlcaponeYou4/17/2023
Cool thanks Dan, I'll give that a shot.
Bbloberenober4/17/2023
So the resulting expression might look like this:
const someColumn = 'col-name';
const value = 10;
sql`${name(someColumn)} < ${value}`;
AAlcaponeYou4/17/2023
sql`${name(someColumn)} < 10`

[{"queryChunks":[{"value":[""]},{"value":"col-name"},{"value":[" "]},"<",{"value":[" "]},"10",{"value":[""]}],"decoder":{},"shouldInlineParams":false}]

When I log the sql, it looks like there's empty chunks. It throws a syntax error near "<"
Bbloberenober4/17/2023
can you enable query logging and post the resulting query that's being run?
AAlcaponeYou4/17/2023
Query: select `id`, `author`, <more columns> from `test` where (`col-name` ? ?) -- params: ["<", "10"]`
Bbloberenober4/17/2023
that's really weird
AAlcaponeYou4/17/2023
the query looks like valid sql
AAlcaponeYou4/17/2023
the queryChunks is odd
AAlcaponeYou4/17/2023
it looks like it's incorrectly parsing the chunks
Bbloberenober4/17/2023
for some reason, it puts < to params list
AAlcaponeYou4/17/2023
oh, that's on purpose
AAlcaponeYou4/17/2023
my comparator is from a variable
Bbloberenober4/17/2023
but you posted this query:
sql`${name(someColumn)} < 10`
AAlcaponeYou4/17/2023
sorry that's right. I'm mixing examples w/ my project
Bbloberenober4/17/2023
if you need the comparator to be a variable, wrap it in sql.raw()
Bbloberenober4/17/2023
so that sql knows it's not a parameter but a SQL piece
AAlcaponeYou4/17/2023
perfect, that fixed it. Thanks a lot!
Bbloberenober4/17/2023
I'm craving proper sql documentation 😖
Bbloberenober4/17/2023
it's really flexible, and poorly documented right now