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.

Check for nested sql query after 'IN' keyword in the Prisma raw query.

See original GitHub issue

Problem

Docs about raw-database-access also guide to use of nested SQL query when using IN keyword, but in the other raw queries use js variables literally. So, many developers have been confused about when to use nested SQL or just js variables if they don’t read the docs carefully. To use myself as an example, I didn’t follow the docs and use them without nested queries and Prisma raw SQL not working in my project without errors, so I had to spend a lot of time fixing them. Obviously, It was my mistake. However, It is not easy to find what is an error in that case because there is no type checking and no error!

Suggested solution

I would like to throw an error with the following message in the following case.

const ids = [1, 3, 5, 10, 20]
const result =
  await prisma.$queryRaw`SELECT * FROM User WHERE id IN (${ids})`
"Expected a value after 'IN' keyword should be the nested sql queries"

Alternatives

Additional context

Prisma uses sql-template-tag package for raw queries. Thus, to fix this issue, after adding this feature to sql-template-tag, Prisma should use the next updated sql-template-tag package. So, I asked about this feature to @blakeembrey, and he tell me his concerns about performance for adding this or additional checks. However, he also says additional checks may have to be executed under a development environment would be fine.

https://github.com/blakeembrey/sql-template-tag/pull/32

Actually, having each logic depending on whether it is a development environment or production environment is the way many projects do.

Thus, checking this condition would be very helpful for debugging and finding errors easily for many developers using Prisma raw queries!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
HwangTaehyuncommented, Oct 20, 2022

@SevInf Oh, thank you for reviewing. I think there is no problem regardless of different dialects when using Sql instance of sql-template-tag after ‘IN’ keyword. Using only js variables after ‘IN’ keyword should be avoided for different dialects if anything. In the PR discussion, Postgres is okay for only a js array variable but MySQL is not. I’m going to think about that point.

1reaction
SevInfcommented, Oct 20, 2022

Hi @HwangTaehyun! We understand that such warning would be a good improvement, however, I have to agree with sql-template-tag maintainers on implementation: simple substring match, similar to what you did in your PR likely will misfire in some cases, (part of them mentioned in PR discussion). Properly parsing SQL without stumbling over specifics of different dialects is would be quite a difficult task and require constant maintenance, which is just not worth it for this feature. We’d be fine with keeping this request open, but at the moment I am not sure how can we approach it without implementing fullu-featured SQL parser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raw database access (Reference) - Prisma
Learn how you can send raw SQL and MongoDB queries to your database using the raw() methods from the Prisma Client API.
Read more >
$rawQuery and nested types #5316 - prisma/prisma - GitHub
I'm using mysql unfortunately. I'll have a look and see if something similar exists. Thanks for the response.
Read more >
How to retrieve a sub query in prisma client with same table?
In Prisma, you would have to use two different queries to solve this: Run an equivalent of the subquery to fetch the weekly_challenge_id ......
Read more >
How To Build a REST API with Prisma and PostgreSQL
Prisma Client: An auto-generated and type-safe query builder. ... Instead of figuring out complex SQL queries or schema migrations, ...
Read more >
Building a SQL Query from Variables with prisma.$queryRaw
So, you can use string variables only for data values, not for identifying database schema or SQL keywords. I knew I didn't want...
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