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)
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
undefinedcan 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
undefinedas the “value” - which indeed does return the same result as if thewherewas 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:
@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
findFirstandwhereclause, but as the app is growing , now it’s becoming a recurring issue.Before, I could by-pass it by using
findFirstOrThrowwhich seemed to have solved the problem and survived with it, but now bothfindFirstandfindFirstOrThrowaren’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
undefinedwhenever thewherecondition is not satisfied, but I am still getting the first record from the table.Note that I am not passing
undefinedornullas search values, but real values from the client appIn my case here,
Addressdefines a relationship between Address and ShopsAny 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
userwithidstring column and copied your code verbatim with the result loggingnullevery 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.
This is currently expected behavior -
undefinedis 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.