prisma: Postgres: Deleting a record that doesn't exist gives a bad error
Problem
The error we get back from bigserial ids it not very nice.
await prisma.types.delete({
where: {
bigserial: "non-existent id",
},
})
PrismaClientKnownRequestError2:
Invalid `prisma.types.delete()` invocation in
/Users/m/Go/src/github.com/prisma/qa-native-types/index.ts:72:30
68 // },
69 // })
70
71 console.log(
→ 72 await prisma.types.delete(
Query interpretation error. Error for binding '0': RecordNotFound("Record to delete does not exist.")
at PrismaClientFetcher.request (/Users/m/Go/src/github.com/prisma/qa-native-types/node_modules/@prisma/client/runtime/index.js:15871:15)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 'P2016',
clientVersion: '2.10.0-dev.58',
meta: {
details: `Error for binding '0': RecordNotFound("Record to delete does not exist.")`
}
}
Suggested Solution
Remove Query interpretation error. Error for binding '0':
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 68
- Comments: 38 (4 by maintainers)
It is quite absurd this doesn’t exist yet even though the issue was opened 3 years ago…
I have this issue as well,
deleteIfExists?It would be great to have something akin to the following for
delete():The current behavior makes things hard to deal with when trying to write idempotent functions for handling Stripe webhooks etc.
While using
deleteMany()is a decent workaround it doesn’t work in cases such as the below:Something akin to the following would be handy as well:
@vuhieutopicus
Hi. I come from the future, and this issue is still open for the next 2 years.
Any updates on this? I have a table who can be referenced in many other tables as fk/ids at the same time, and those tables also contains other relations, i can’t update those nested relations, can’t upsert them, can’t delete them, can’t connect, can’t disconnect, not to talk about the amount of logic to determine if a object should be deleted or just updated because no operation on prisma works as described on the documentation.
A(id: 1, b: B(id: refA(1),attrs: [C(id: refAFromB(1)),C(id: refAFromB(1))]), f: F(id: refA(1), g:G(id: refA(1), h:H(id: refAFromG(1)))),);
Until now, my work arounds were based on creating transactions to slowly fix some relations until it is okay to finally update my main entity, but i guess that’s not only me based on the amount of comments here.
Here is the work around i found, have in mind i am posting this code so you guys can have an example of the problem related with trying to delete a register that doesn’t exist during a transaction (nested queries):
The strategy is: 1 - compute simple data to be updated; 2 - computed inner relationships that block deletes from happening (even with the cascade on schema); 3 - check on db all current 1x1 that exist; 4 - add the nested delete clause only if it does; 5 - update the main entity; 6 - rehydrate nested relations that shouldn’t be deleted (we should also have the ability to do that within prisma config obj in my opinion);
you can catch it until the
delete if not existsis implemented is changedAny update?
deleteMany- is NOT a workaround because it doesn’t work for1 <-> 1relations (works for1 <-> N)try <-> catch- is NOT a workaround because it doesn’t work for nesteddeleteany updates?
Same issue here as well. Feel like it shouldn’t require a different method, just handle more gracefully?
@mindlid Based on the above comment, I’m using it as follows.
My work around for this was to change
deletefordeleteMany. Hope it helpsFacing a similar issue as well:
Right now I’m catching it in a
try-catchbut would love to be able to have a similar function asupsertbut for deletes 😃Are there any workarounds available for one-to-one relationships?
It ain’t pretty but works for me:
On my relation, I use
onDelete: Cascadeto also remove all relational registers.If you are using nestjs, a better approach would be using an exception filter, you would not neet try/catching all around your code, just enable the filter globally. Check it out:
references: https://www.prisma.io/docs/reference/api-reference/error-reference https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions https://www.prisma.io/blog/nestjs-prisma-error-handling-7D056s1kOop2
We have the same happening to us on MariaDB with ints: Prisma version:
2.22.1As a workaround we are using
deleteMany#9460 doesn’t fully cover everything because
deleteManydoesn’t work for1 <-> 1relations (unless the suggestion is to cover1 <-> 1and1 <-> NandN <-> NwithdeleteIfExists). It also doesn’t matter if we know the error code, thistry <-> catchdoesn’t work if we do a nesteddelete.If we could cover these cases (https://github.com/prisma/prisma/issues/4072#issuecomment-1557486529) without throwing an error it would be perfect. Ideally, when the record to delete doesn’t exist it should just ignore that and proceed further.
I am encountering the same annoying issue, when updating a record and passing
delete: { true },for one of the relations.I’m facing the same issue in this simple example, I don’t really care if it exists or not I just want to make sure its deleted before creating a new one. I think opting out so that delete doesn’t throw might be a good approach.
I would recommend against using this approach, as now any error, even a temporary network issue, will be treated the same as “the database answered that there is no record with id $id”…
Checking for
P2025orP2016seems like a better interim solution…+1
I’m using deleteMany for now, since the email field is unique, I know it’s a safe operation