K
Kysely•11mo ago
SneakOnYou

issue with complied sql when doing + interval operation

Hey, I have a query that is generated using kysely where I select the following field sql that I get as a string from a file: fields = [close_date + interval '12 month'] what happens is that when I do .select(fields) the close_date + interval '12 month' gets wrapped in double quotes, what is the best way to correct this postgres
14 Replies
koskimas
koskimas•11mo ago
Could you provide the query using https://kyse.link?
SneakOnYou
SneakOnYou•11mo ago
SneakOnYou
SneakOnYou•11mo ago
ok but I have a list of sql queries and I want to add them to the select clause and give each a different alias
SneakOnYou
SneakOnYou•11mo ago
fields = ["close_date", "close_date + interval '12 month'"]
fields.map((field) => `${field} as ${field}_new)`
fields = ["close_date", "close_date + interval '12 month'"]
fields.map((field) => `${field} as ${field}_new)`
koskimas
koskimas•11mo ago
That's going to be very difficult to do in a type-safe way You can of course drop down to Kysely<any> and write whatever query you want, but the output type will not be automatically inferred
SneakOnYou
SneakOnYou•11mo ago
ok then what should I do ?
koskimas
koskimas•11mo ago
Switch to drizzle?
SneakOnYou
SneakOnYou•11mo ago
drizzle ? please explain @koskimas
koskimas
koskimas•11mo ago
You can do something like this https://kyse.link/?p=s&i=767sswpj75AFEMo2DaZf, but I'm guessing this is not the whole query you want to build. You probably need to use Kysely<any> for this. And actually the types are completely wrong in my example. For dynamic set of selections with dynamic aliases, you just need to forget type-safety and manually type the result.
SneakOnYou
SneakOnYou•11mo ago
I will try the thing you suggested in the link after my meeting 🙂
koskimas
koskimas•11mo ago
So you were on a meeting but insisted that I help you immediately. Wow dude. I think I'm done
SneakOnYou
SneakOnYou•11mo ago
I just when into the metting 6 minutes ago man, sorry for that I tagged you on xx:13 my meeting was at xx:15 this didn't work, getting the same format as it had at the beginning with the double quotes @koskimas I used your solution with cobination of the following:
.select((eb) => fields.map((field) => sql`${sql.raw(field.fieldSql)}`.as(`${field.fieldAlias}`)));
.select((eb) => fields.map((field) => sql`${sql.raw(field.fieldSql)}`.as(`${field.fieldAlias}`)));
this worked
Want results from more Discord servers?
Add your server
More Posts