I have a question this query is working in pgadmin...
# orm-help
g
I have a question this query is working in pgadmin however it doesn't work in prisma.$queryRaw
Copy code
const queryCompanies = await prisma.$queryRaw`SELECT * FROM "Company" where "name" like '%${searchName}%' and "companyId" in (SELECT "company_id" FROM "staff" where "userId"=${user.user_id} ) OFFSET ${iSkip} ROWS FETCH NEXT ${limit} ROWS ONLY  ; `
I am searching by searchName
r
@genedy 👋 What’s the error you get when using
$queryRaw
?
g
search name format in the query
I couldn't write it I have the same query without search name and it works
r
Try including the
%
inside the
searchName
variable and check
g
Copy code
const searchKey = `%${searchName}%`
            const queryCompanies = await prisma.$queryRaw`SELECT * FROM "Company" where "name" like "%${searchKey}%"and "companyId" in (SELECT "company_id" FROM "staff" where "userId"=${user.user_id} ) OFFSET ${iSkip} ROWS FETCH NEXT ${limit} ROWS ONLY  ; `
@Ryan do you mean like that ?
Copy code
const searchKey = `%${searchName}%`
            const queryCompanies = await prisma.$queryRaw`SELECT * FROM "Company" where "name" like '${searchKey}' and "companyId" in (SELECT "company_id" FROM "staff" where "userId"=${user.user_id} ) OFFSET ${iSkip} ROWS FETCH NEXT ${limit} ROWS ONLY  ; `
r
Remove the single quotes, they are not needed
g
Copy code
const searchKey = `%${searchName}%`
            const queryCompanies = await prisma.$queryRaw`SELECT * FROM "Company" where "name" like ${searchKey} and "companyId" in (SELECT "company_id" FROM "staff" where "userId"=${user.user_id} ) OFFSET ${iSkip} ROWS FETCH NEXT ${limit} ROWS ONLY  ; `
@Ryan same result
r
What is the value of
searchKey
? It’s working fine for me.
g
const searchKey =
%${searchName}%
r
The query runs fine. I think there might be an issue with another parameter in this case.
g
I solved it . and there was no error in the parameters . the problem was in queryRaw itself
I used this condition .
Copy code
prisma.$queryRaw``
and this was wrong
the correct was to use prisma.$queryRaw()
💯 2
I know that first is working , but second is more valid in complex query.
I am feeling thankful for you @Ryan
👍 2