jest-prisma icon indicating copy to clipboard operation
jest-prisma copied to clipboard

$queryRaw doesn't create prepared statements correctly

Open Noor0 opened this issue 11 months ago • 1 comments

function getUsers(ids: number[], filter?: string) {
  const filterStatement = `%${filter}%`
  return $queryRaw`SELECT * FROM users WHERE id IN (${Prisma.join([ids])}) ${filter ? Prisma.sql`OR email LIKE ${filterStatement}` : Prisma.empty}`
}

The query above works fine outside of tests but, in a test with jest-prisma It creates a bad prepared-statement that looks like this in the logs

postgres-1  | 2024-03-05 08:10:26.809 UTC [348] ERROR:  syntax error at or near "$2" at character 45
postgres-1  | 2024-03-05 08:10:26.809 UTC [348] STATEMENT:
postgres-1  | 	      SELECT * FROM users WHERE id IN ($1) $2
postgres-1  |

and results in a PrismaClient error

PrismaClientKnownRequestError:
    Invalid `prisma.$queryRaw()` invocation:


    Raw query failed. Code: `42601`. Message: `ERROR: syntax error at or near "$2"`

Noor0 avatar Mar 05 '24 08:03 Noor0

same issue here. it seems that nested sqltag expression like this does not work as expected

${id? Prisma.sql`and c.id != ${id}` : Prisma.empty}

shu-bc avatar Aug 30 '24 03:08 shu-bc