There is an issue with Prisma which causes concurrent usages of findUnique()
to return null values if the calls specify the uniqueWhereInput keys in different orders
Tracked Issue: Prisma Issue #16267
- Only happens with
findUnique
orfindUniqueOrThrow
- Only when multiple
findUnique
calls are made concurrently and Prisma runs them in a batched query. - The uniqueWhereInput needs to be different for the problem to occur
Example:
const funcA = () =>
prisma.relationship.findUnique({
where: {
fooId_barId: {
// note the order of these two keys
fooId,
barId,
},
},
});
const funcB = () =>
prisma.relationship.findUnique({
where: {
fooId_barId: {
// note the order of these two keys
barId,
fooId,
},
},
});
const test1 = await Promise.all([funcA(), funcA()]);
// test1[0]: found
// test1[1]: found
const test2 = await Promise.all([funcB(), funcB()]);
// test2[0]: found
// test2[1]: found
const test3 = await Promise.all([funcA(), funcB()]);
// test3[0]: found
// test3[1]: null (!!)
const test4 = await Promise.all([funcB(), funcA()]);
// test4[0]: found
// test4[1]: null (!!)
runTest 1: consistent order
{
seqRow1: true,
seqRow2: true,
concurrentRow1: true,
concurrentRow2: true
}
runTest 2: different, but still consistent order
{
seqRow1: true,
seqRow2: true,
concurrentRow1: true,
concurrentRow2: true
}
runTest 3: differing orders
{
seqRow1: true,
seqRow2: true,
concurrentRow1: true,
concurrentRow2: false
}
!!!!!!!! Error: concurrent usage did not match
runTest 4: reverse but still different orders
{
seqRow1: true,
seqRow2: true,
concurrentRow1: true,
concurrentRow2: false
}
!!!!!!!! Error: concurrent usage did not match
- Be consistent with all usages of
findUnique
- Switch one of the concurrent calls to use
findFirst
instead
- 4.0.0
- 4.5.0
- 4.6.1
- Clone this repository
- Change the credentials in the
.env
file - Run
npm install
to install dependencies - Run
npm run prisma migrate dev
to bootstrap database - Run
npm run start
to reproduce