Hey, is there an easy way of doing an update `only...
# random
r
Hey, is there an easy way of doing an update
only if
a record exists? Currently I have to first check if it exists & then update, can we do it in one query with prisma? One option is ofcourse try-catch but was curious if some better method exists
i
I'm afraid it's impossible with just an update. Docs say update returns either an updated record or throws NotFoundError.
r
Yeahh, is it worth opening an issue for this tho? If something like
updateIfExists
is implemented, its gonna make my life easier.
i
I also thought about it the other day. I think it's not very convenient to throw an error in this case. Raw SQL would return either 0 as number of updated rows, or an empty set if
RETURNING
clause used. Thus, I'd rather expect
update
to return an empty array for example.
n
A Feature Request for this use case exists: #14009 🙂
r
Upvoted, thanks for sharing @Nurul!
@Irek Prucnal Hmm makes sense, well an empty array also should work, let me explore a little and see if something better comes to my mind. Thanks for your help btw!
👍 1
c
Have you tried using
updateMany()
? I don't think it errors if no records are found, should just return zero updated.
Your
where
would just be
id: whatever
so obviously only one record would ever get updated.
r
Interesting approach, i’ll give it a try. Thanks!