Search code examples
javascriptprisma

How to filter multiple values and nulls in prisma


Im having some trouble defining a query with prisma. When selecting values from a table with 2 different values, lets say name and age, how can I filter that table with name equals to "John" or null, and age equals to 22 or null?

Edit : trying to provide some code for some context, here is something I tried :

const results = await prisma.myModel.findMany({
  where: {
    OR: [
      {
        name: {
          in: ["John", null], // Include "John" and NULL names
        },
        age: {
          in: [22, null], // Include ages 22 and NULL
        },
      },
    ],
  },
});

However, the in statement, according to the prima documentation, excludes all nulls, therefore the query will not contain the NULL entries


Solution

  • I believe that is should be the following:

    const whereNameAndAgeIs = Prisma.validator<Prisma.UserWhereInput>()({
      AND: [{
        OR: [{
          name: 'John'
        }, {
          name: null
        }],
      }, {
        OR: [{
          age: 22,
        }, {
          age: null
        }],
      }],
    })
    

    Check out the Prisma docs: