question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$queryRaw doesn't work with single quotes

See original GitHub issue

Discussed in https://github.com/prisma/prisma/discussions/9991

<div type='discussions-op-text'>

Originally posted by bardiaheshmati October 28, 2021 i have a query that goes

select count(id), status,  "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = ${id}) and "createdAt" > current_date - interval '${interval} day' group by "createdAt"::date, status;

i’ve also tried user Prisma.sql to pass the value inside the quotes but it keeps throwing error that it expected 1 argument but fount 2.

i did not have this issue with prisma 2.20.1 this issue is only happening on version 3.3.0

Query: select count(id) as count, status,  "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = $1) and "createdAt" >  current_date - interval '$2 day' and key notnull group by "createdAt"::date, status
Param: ["main","30"]
PrismaClientKnownRequestError:
Invalid `prisma.queryRaw()` invocation:


  Your raw query had an incorrect number of parameters. Expected: `1`, actual: `2`.
  code: 'P1016',
  clientVersion: '3.3.0',
  meta: { expected: 1, actual: 2 }
}

any suggestions ? i’m not sure if this is a bug.

</div>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Weakkycommented, Apr 26, 2022

Hey folks,

@jschuur I can confirm this is a SQL prepared statement limitation. Your query parameter is interpreted as a text because it is inside single quotes. I see two ways you could make this work (without using $queryRawUnsafe):

  1. Either with the solution suggested above. Pass ${"7 day"} as query parameter
  2. Use concatenation operators if you really wish to make "day" static. eg 👇
    await prisma.$queryRaw`SELECT CURRENT_TIMESTAMP - (${7} || ' day')::INTERVAL;`
    

With that, I’m gonna close the issue as there’s nothing we can do about it.

Thanks for reporting! 🙏

1reaction
janpiocommented, Dec 29, 2021

Thanks, with this information we can take a deeper look @jschuur 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

prisma $queryRaw doesn't work with single quotes in the string
sql to pass the value inside the quotes but it keeps throwing error that it expected 1 argument but fount 2. i did...
Read more >
Prisma $queryRaw cannot grab any tables added from migration
Hey there, I have a web app I've been working on, ... seems also wrapping 'Tournament' in double quotes fixed the issue, so...
Read more >
Dealing with single Quote in SQL Query
Hi Guys. how do I deal with single Quote inside string in an SQL query? currently this type of query is failing with...
Read more >
Raw database access (Reference) - Prisma
Type caveats when using raw SQL. When you type the results of $queryRaw , the raw data might not always match the suggested...
Read more >
[Solved]-Need replace text with single quotes in SQL-postgresql
Coding example for the question Need replace text with single quotes in SQL-postgresql. ... prisma $queryRaw doesn't work with single quotes in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found