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' } ]
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.
Most helpful comment
Thanks! Somehow I missed that.