prisma: `findFirst()` should not return result if `where` condition doesn't match

Bug description

findFirst query returns the first row from table even if where condition doesn’t match

How to reproduce

Query

const user = await prismaClient.user.findFirst({
        where: { id: undefined }
      });

Result:

{ id: 1 }

Expected behavior

I would expect the findFirst query with where parameter doesn’t return a result if where condition doesn’t match with any records.

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

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

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL 14.1
  • Node.js version: 13.14.0

Prisma Version

"@prisma/client": "^3.7.0",
"prisma": "^3.7.0",
Prisma CLI version: prisma/1.34.0 (darwin-x64) node-v10.4.1

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 17
  • Comments: 27 (5 by maintainers)

Most upvoted comments

I had this trip me up as well. I would say this should be tagged as a bug not as support.

Unless this is expected behavior? But that seems dangerous/incorrect to return an item that doesn’t match and then we would have to write more code to validate the returned item is the one requested.

THAT indeed is pretty scary. Can you quickly open a new issue with all the information it asks for? Then we can label that correctly and take a look at reproducing this.

As discussed just above, values being undefined can lead to some confusing behavior in Prisma ORM. We have just released a new Preview feature which hopes to address that.

I’m leaving this issue open, but please let me know if this Preview feature helps your use cases!

We never got an issue showing us that a query for data that does not exist would return different or random data. We are very certain that this is technically impossible.

The original issue here is about doing a query with undefined as the “value” - which indeed does return the same result as if the where was not supplied at all. And that is intended behavior right now.

No, they were undefined. Yes, you are right, my bad. So, the query is correct in this case 👍 Thank you @jonknyc

Is it possible you are passing undefined for data.phone and data.password?

On Tue, Sep 10, 2024 at 10:35 PM João Paulo Constantino < @.***> wrote:

Same issue here.

const user = await a.user.findFirst({ where: { phone: data.phone, password: data.password, },});

Got this query: SELECT “public”.“User”.“id”, “public”.“User”.“phone”, “public”.“User”.“password”, “public”."User ".“name”, “public”.“User”.“isPhoneVerified” FROM “public”.“User” WHERE 1=1 LIMIT $1 OFFSET $2

The params were: Params: [1,0].

Since we add value in the where clause, no matter which value it is, undefined, null, etc. For me, doesn’t make sense to get this where 1=1, in my case the param values were an empty object {}.

— Reply to this email directly, view it on GitHub https://github.com/prisma/prisma/issues/11021#issuecomment-2342495871, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABM6IQMEW23VBPBVHWZRG4DZV6UAJAVCNFSM5LQAGJB2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMZUGI2DSNJYG4YQ . You are receiving this because you commented.Message ID: @.***>

@ElijahBus https://www.prisma.io/docs/orm/prisma-client/observability-and-logging/logging#event-based-logging gives you all the information, you “just” have to combine them together in your head or via quick copy-paste.

Hey team ! I think this issue should get as much attention as possible , it’s super scary and dangerous !

I am on Mac OS , "@prisma/client": "5.5.2", node v21.6.1 .

At first, I had no issues using findFirst and where clause, but as the app is growing , now it’s becoming a recurring issue.

Before, I could by-pass it by using findFirstOrThrow which seemed to have solved the problem and survived with it, but now both findFirst and findFirstOrThrow aren’t working as expected.

not sure whether the problem is the naming of the methods or the functionality behind, or the documentation not being explicit enough, or me not understanding how the API works

I am expecting the returned value to be undefined whenever the where condition is not satisfied, but I am still getting the first record from the table.

Note that I am not passing undefined or null as search values, but real values from the client app

this.prisma.shops.findFirst({
      where: {
        Address: {
          shop_no: shopNumber,
        },

In my case here, Address defines a relationship between Address and Shops

Any assistance ??

I’m not able to reproduce this behavior, which is good because this would be a scary bug touching user login and authentication for my app.

I also have a user with id string column and copied your code verbatim with the result logging null every time.

Oh wait sorry i got what you mean. Passing in the value for id of undefined.

But what i mean/meant to report is with a valid id. And it doesn’t exist in the table this will return a random item. And not null or undefined when not found.

const user = await prismaClient.user.findFirst({
        where: { id: "abc123" }
      });
      
      
// user will be a random user when this id abc123 doesn't exist and is not found

user: {id: "xyz"}

This is currently expected behavior - undefined is treated as the value not being present at all. Check the issues under this label: https://github.com/prisma/prisma/labels/topic%3A null vs undefined We are aware that this is not super intuitive, but also do not have a good instant solution right now so are collecting issues for now.