- Più recenti
- Maggior numero di voti
- Maggior numero di commenti
What you are trying to do cannot be done with a simple ConditionExpression
. Conditions are only evaluated on the item on which you are accessing, not on any other item in the table. For example, you want to base your condition on item Andy(UserId) 123(UUID) PENDING
but you are writing with item Andy(UserId) 444(UUID) PENDING
, to DynamoDB these are 2 completely different items and that is why you are not seeing the expected behaviour from your conditional writes.
This blog will help you to implement your logic in a different manner, however, its not so straight forward:
Looking at your original ask:
Write Request: Andy(UserId) 123(UUID) PENDING Only allow write success if there no existing Andy(UserId) PENDING in the table already, that is, at any time, the table should only have one record with Andy(UserId) PENDING.
I think the condition statement should be:
attribute_not_exists(UserId) or (attribute_exists(UserId) and UserStatus <> :pendingstatus)
As I read this, it means:
- Allow access when the UserId doesn't exist or
- Allow access when the UserId does exist and the UserStatus is not PENDING
I tested this on a database and it allows me to add a new user; I cannot update a user when the status is PENDING; but I can update a user when the status is anything else.
Also: Note that Status
is a reserved word in DynamoDB so you should consider using another attribute name - for my table I used UserStatus
instead. While you can workaround and user a keyword it's easier not to.
Contenuto pertinente
- AWS UFFICIALEAggiornata un anno fa
- AWS UFFICIALEAggiornata 3 anni fa
- AWS UFFICIALEAggiornata 2 anni fa
Hi, this condition does not seem to work. I tested it with below case: Record already in DB:
Andy(UserId) 123(UUID) PENDING
Try to insert record:
Andy(UserId) 444(UUID) PENDING
Expected: failure, as Andy already exist and in PENDING state Actual: successfully inserted record
I don't know what to say - I tested it here and that condition won't let me put another user in with the same
UserId
where the status isPENDING
. If the status is something else, it works fine. The one change I made was to change the attributestatus
toUserStatus
so that it isn't a DynamoDB reserved word. I know that you can work around this but it's easier not to have to do that. Answer updated to reflect that.