Empty objects in `where`-clause make the query return nothing
See original GitHub issueBug description
I craft a where-clause for my query by transforming my own filter type from a GraphQL resolver into the Prisma format. When my filter (which is an array of objects) is only an empty objects, the query returns no results.
How to reproduce
My code works like this:
// I have a model named "Permissions" which i craft the where-clause for
// "filters" is my simple list of custom filter objects
const filter: Prisma.PermissionWhereInput = {
OR: filters?.map(
(f): Prisma.PermissionWhereInput => ({
AND: {
identifier: filterString(f.identifier),
timeCreated: filterDateTime(f.timeCreated),
timeModified: filterDateTime(f.timeModified),
},
})
),
};
// I apply that filter to my query
const data = await database.permission.findMany({
// filtering
where: filter,
});
Expected behavior
The documentation says that queries containing objects with undefined
as values for their keys are ignored.
When not defining “filters” at all, this is the resulting query filter:
The query log contains this as the where-clause: WHERE 1=1
When defining filters
as an empty object, this is the resulting query filter:
The query log now contains an always-false where-clause: WHERE 1=0
,
even though I’d expect it to still filter as if there was no filter at all.
This behavior is also appearing for other models from my PrismaClient.
Environment & setup
- OS: macOS
- Database: PostgreSQL inside docker
- Node.js version: 15
- Prisma version:
@prisma/cli : 2.13.1
@prisma/client : 2.13.1
Current platform : darwin
Query Engine : query-engine fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at ../node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at ../node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at ../node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at ../node_modules/@prisma/engines/prisma-fmt-darwin)
Studio : 0.329.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top GitHub Comments
Hey @PhilippMolitor, I tried simplifying your example a bit:
Returns all posts. This is expected because:
Also returns all posts. What’s surprising is that:
Returns no posts. Is that correct?
Okay, I learned a bit more about the rationale.
Conditionals with no values are ambiguous: “OR nothing” could mean return everything or it could mean return nothing. Same with AND and NOT. In hindsight, we probably should have disallowed this state.
But given that this behavior is allowed, I don’t think this is worth changing because it could go either way (return all or return none) and people that rely on the current behavior will be very surprised.
Here are the rules:
I’ve open an issue to document this: https://github.com/prisma/docs/issues/1315