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 generates +1 other queries for every middleware

See original GitHub issue

Bug description

The prisma.$queryRaw() command generates 4 queries instead of 1.

How to reproduce

$queryRaw() generates 4 queries for one operation!? My example:

await prisma.$queryRaw`SELECT id FROM public.user`

Log output:

prisma:query SELECT id FROM public.user
prisma:query SELECT id FROM public.user
prisma:query SELECT id FROM public.user
prisma:query SELECT id FROM public.user

Expected behavior

It should generate 1 query for 1 operation

Prisma information

model User {
  id                Int       @id @default(autoincrement())
}

I have 4 middleware functions.

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: v16.13.1

Prisma Version

"^3.8.1"

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
georgekraxcommented, Jan 19, 2022

Thank you very much for your support! Yes, it did work out. If you and your team at Prisma continue to provide such an amazing support, you will definitely make a difference. It shows that you are interested in each user’s issue, and changes the experience all together.

1reaction
janpiocommented, Jan 18, 2022

Ok, got it:

const checkParams: Prisma.Middleware<any> = async (params, next) => {
  if (!params.model || !checkModel(params.model)) return next(params);
};

Your middlewares are all calling this method as their first action - and on a raw query params.model is indeed undefined and you already trigger the query by calling next. Then later at the end of the middleware you call next again.

Does that make sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raw database access (Reference) - Prisma
$queryRaw returns actual database records. For example, the following SELECT query returns all fields for each record in the User table:.
Read more >
Query Builder - Laravel - The PHP Framework For Web Artisans
Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database ......
Read more >
Raw Queries - Sequelize
Several other query types are available. ... await sequelize.query('SELECT 1', { ... Will get called for every SQL query that gets sent
Read more >
node.js - execute Middleware function for all query middlewares
looks like there is no such possibility. you might construct it dynamically. you could grab a list of methods here: ...
Read more >
SQL Queries - EF Core - Microsoft Learn
Using SQL queries in Entity Framework Core. ... since databases do not allow parameterizing column names (or any other part of the schema)....
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