Muting create exceptions when item already exists whilst preserving other exceptions #439
dmeehan1968
started this conversation in
General
Replies: 1 comment
-
I understand your use case. I'm not sure what the set of reasonable exceptions to to ignore would be and how to do that flexibly for all use cases. If others have similar, but different exceptions to mute, please chime in here. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a use case for being able to issue Model
create()
calls where there is a strong possibility that the record already exists (incoming data deliberately has duplicates and there is no point in overwriting the existing record (i.e. update/upsert).I can set
params.throws = false
but this seems likely to mute other exceptions. As a result, I've been using try/catch and handling the case of a OneTableError with code = 'ConditionalCheckFailedException', and treating that as a duplicate that can be ignored.My table has a primary PK that is the
devId
, and the SK is thedeviceTime
as an ISO string. The combination tells us that its a duplicate (history: index
allows us to know which duplicate actually created the item so we can track losses).Here is an example using try/catch:
And here is the equivilent
throws: false
which is cleaner, but potentially mutes other exceptions:It seems that
permit.exists
is meant to address this, but I found that:permit.exists = false
means the create only succeeds without exception if the item doesn't existpermit.exists = true
means the create only succeeds without exception if the item does existpermit.exists = null
means that the item is updated or inserted, rather than left alone if already existingIt seems like there is case for another param such as 'ignoreDuplicates' that handles this case more elegantly, but maybe I'm missing something.
Whilst a condition could be added to compare the fields and avoid the update, this would still raise an exception when its not exceptional (and
params.throws = null
might mute other exceptions).Or in other words, there doesn't appears to be a way of saying "if this primary key already exists its not an exception, and I want to leave it alone"
Beta Was this translation helpful? Give feedback.
All reactions