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.

Empty objects in `where`-clause make the query return nothing

See original GitHub issue

Bug 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: image 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: image 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
matthewmuellercommented, Feb 5, 2021

Hey @PhilippMolitor, I tried simplifying your example a bit:

const posts = await prisma.post.findMany({
  where: {
    OR: undefined
  }
})

Returns all posts. This is expected because:

const posts = await prisma.post.findMany({
  where: {
  }
})

Also returns all posts. What’s surprising is that:

const posts = await prisma.post.findMany({
  where: {
    OR: []       // or OR: [{ AND: {} }]
  }
})

Returns no posts. Is that correct?

1reaction
matthewmuellercommented, Feb 16, 2021

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:

Name 0 Filter 1 Filter 2 Filter
OR return empty list validate single filter validate all filters
AND return all items validate single filter validate all filters
NOT return all items validate single filter validate all filters

I’ve open an issue to document this: https://github.com/prisma/docs/issues/1315

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null or empty object when LINQ to Entities query returns nothing
Database queries return result sets. An empty set is a reasonable answer; it means you don't have any of the things searched for....
Read more >
How to return a empty object when linq query yields nothing ...
I'm trying to query the database using linq query and the FirstOrDefault yields null value. Is there a way I can return a...
Read more >
When mysql WHERE clause is empty, return all rows
Now if $randomvariable is empty (nothing), I would like it to return all rows. Currently if it's empty it returns nothing, because it...
Read more >
How to SELECT Records With No NULL Values in MySQL
By far the simplest and most straightforward method for ensuring a particular column's result set doesn't contain NULL values is to use the...
Read more >
Why do multiple Where clauses return an empty data set?
When you add a Where clause to the definition of an object, the restriction is added to the restrictions set by the joins...
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