Prisma notIn: [] matches every row, so an empty list can delete a whole table
- Field note
Measured against a table with three rows:
where on id |
Rows matched |
|---|---|
{ notIn: [] } |
3 |
{ in: [] } |
0 |
{ in: undefined } |
3 |
{ notIn: undefined } |
3 |
{ notIn: ['a'] } |
2 |
deleteMany({ where: { id: { notIn: [] } } }) deleted all three.
The result is logical: nothing is excluded, so everything matches. It becomes data loss when the list comes from another system. I hit this with an API that returned 200 with items: [] and success: false. The code read items and took a degraded reply to mean "this customer has no records".
A check such as if (orphans.length === 0) return doesn't help, because in this case the orphan count is the entire table. Guard on the ratio instead, above a small floor so a three-row tenant losing one row doesn't trip it:
if (orphans.length >= 5 && orphans.length / total > 0.2) {
throw new Error('refusing to delete most of the set')
}in: undefined is the sibling trap. Prisma treats undefined as "ignore this field", so a tenant filter whose array is accidentally undefined returns every tenant's rows.