Dev__
07/05/2021, 12:51 PMORDER BY
this.prisma.$queryRaw`
SELECT *
FROM "public"."OrderStatus"
INNER JOIN "public"."OrderStatusDescription" ON ("public"."OrderStatus"."id" = "public"."OrderStatusDescription"."orderStatusId")
WHERE "public"."OrderStatusDescription"."languageId" = ${languageId}
ORDER BY "public"."OrderStatusDescription"."name" DESC LIMIT ${pagination.take} OFFSET ${pagination.skip}
`
the moment I have a variable instead of hardcoded DESC the query doesnt work.
ORDER BY "public"."OrderStatusDescription"."name" ${order.toUpperCase()} // order = 'asc' | 'desc'Dev__
07/05/2021, 1:00 PMDominic Hadfield
07/05/2021, 1:01 PMSELECT …Dominic Hadfield
07/05/2021, 1:01 PM'ASC' instead of ASCDev__
07/05/2021, 1:04 PMDev__
07/05/2021, 1:04 PMDominic Hadfield
07/05/2021, 1:06 PMDominic Hadfield
07/05/2021, 1:07 PMDominic Hadfield
07/05/2021, 1:07 PMWHERE "public"."OrderStatusDescription"."languageId" = "${languageId}"Dev__
07/05/2021, 1:09 PMDev__
07/05/2021, 1:09 PMDominic Hadfield
07/05/2021, 1:14 PMDominic Hadfield
07/05/2021, 1:14 PMDev__
07/05/2021, 1:21 PMDev__
07/05/2021, 1:21 PMOrderStatusDescription table but prisma doesnt support this so I want to do it with a raw queryRyan
07/05/2021, 1:29 PMasc or desc dynamically:
await prisma.$queryRaw`select * from table order by field ${
order === 'desc' ? Prisma.sql`desc` : Prisma.sql`asc`
}`Dev__
07/05/2021, 1:36 PM${Prisma.sql`${order}`}Dev__
07/05/2021, 1:38 PMORDER BY "public"."OrderStatusDescription"."name"
example
ORDER BY "public"."OrderStatusDescription"."${orderBy}"
but I keep getting column doesnt exists. how would I solve this one?Dev__
07/05/2021, 1:38 PMRyan
07/05/2021, 1:51 PMyes thanks, that works! by why didnt this workRaw query support is based on this library and whatever you interpolate inside is taken as a value, which is why you need to do this.
also, I am trying to selected the field dynamicallyThat’s tricky. I would need to check for this.
Ryan
07/05/2021, 1:53 PMlet order = 'desc'
let field = 'id'
await prisma.$queryRaw`select * from User order by ${Prisma.raw(field)} ${
order === 'desc' ? Prisma.sql`desc` : Prisma.sql`asc`
}`Dev__
07/05/2021, 1:57 PM${Prisma.raw(orderBy)}