Below are what I have tried,
let result = await db.$queryRaw`SELECT * from "Person"` // This works fine
console.log(result)
const model = 'Person'
result = await db.$queryRaw`SELECT * from "${model}"` // This throws the error
The second $queryRaw
throws the following error.
Invalid `prisma.queryRaw()` invocation:
Raw query failed. Code: `42P01`. Message: `relation "$1" does not exist`
Below are Prisma logs from two queryRaw
runs.
{"level":20,"time":1649915257507,"pid":6135,"hostname":"MBP","prisma":{"clientVersion":"3.11.1"},"timestamp":"2022-04-14T05:47:37.507Z","query":"SELECT * from \"Person\"","params":"[]","duration":2,"target":"quaint::connector::metrics","msg":"Query performed in 2 msec"}
console.log
[
{
id: 29,
name: 'Rob',
email: 'rob@example.com',
age: 30,
roles: null
}
]
{"level":20,"time":1649915257513,"pid":6135,"hostname":"Chens-MBP","prisma":{"clientVersion":"3.11.1"},"timestamp":"2022-04-14T05:47:37.513Z","query":"SELECT * from \"$1\"","params":"[\"Person\"]","duration":1,"target":"quaint::connector::metrics","msg":"Query performed in 1 msec"}
As we can see, the $1
parameter is set correctly in the second queryRaw
run. But for some unknown reasons, it throws the relation "$1" does not exist
error.