Prisma1: The `OR` operator in X`WhereInput` of prisma client behaves like `AND` for certain operands

Created on 23 Dec 2018  路  2Comments  路  Source: prisma/prisma1

Bug Description

The OR operator in XWhereInput of prisma client behaves like AND for certain operands.

To Reproduce

A minimal example with reproduction steps is provided here: https://github.com/jhanschoo/prisma-client-or-bug

I provide excerpts of the most pertinent files below:

datamodel.prisma

type User {
  id: ID! @unique
  macGuffins: [MacGuffin!]!
}

type MacGuffin {
  id: ID! @unique
  owners: [User!]!
}

index.js

const { prisma } = require('./generated/prisma-client/index');

async function main() {
  prisma.deleteManyUsers();
  prisma.deleteManyMacGuffins();
  const user1 = await prisma.createUser({});
  const user2 = await prisma.createUser({});
  const macGuffin1 = await prisma.createMacGuffin({
    owners: { connect: { id: user1.id } }
  });
  const macGuffin2 = await prisma.createMacGuffin({
    owners: { connect: { id: user2.id } }
  });
  const queryMcguffins1 = await prisma.macGuffins({ where: {
    OR: {
      id: macGuffin2.id,
      owners_some: { id: user1.id }
    }
  }});
  console.log(queryMcguffins1);
  const queryMcguffins2 = await prisma.macGuffins({ where: {
    OR: {
      owners_some: { id: user1.id }
    }
  }});
  console.log(queryMcguffins2);
}

main().catch(e => console.error(e));

Example output:

$ node index.js
[]
[ { id: 'cjq0vvfkj9gkj0a61nwxfdoj6' } ]

Expected output (example)

$ node index.js
[ { id: 'cjq0vvfkj9gkj0a61nwxfdoj6' } ]
[ { id: 'cjq0vvfkj9gkj0a61nwxfdoj6' } ]
bu1-repro-available areclient

Most helpful comment

Thanks! Somehow I missed that.

All 2 comments

Hi @jhanschoo,

I believe that you are misunderstanding the API. The things that are in a single object are joined by AND, and you need to pass the conditions as an array to join them by OR like so:

const queryMcguffins1 = await prisma.macGuffins({ where: {
   OR:[
     {
      id: macGuffin1.id
     },
     {
      owners_some: {
        id: user1.id
      }
     }
   ]
  }});

I believe the cause of this confusion is that you were using autocompletion to guess the API as this documented nicely here: https://www.prisma.io/docs/prisma-client/basic-data-access/reading-data-JAVASCRIPT-rsc2/#examples.

But still, this is good feedback on the API of the client. /cc @timsuchanek

I am closing this right now as the problem has been resolved but we may discuss how we can make this more explicit in the future.

Thanks! Somehow I missed that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schickling picture schickling  路  3Comments

akoenig picture akoenig  路  3Comments

hoodsy picture hoodsy  路  3Comments

dohomi picture dohomi  路  3Comments

marktani picture marktani  路  3Comments