DynamoDB:ConditionalCheckFailedException | Status Code: 400; Error Code: ConditionalCheckFailedException

ashishdonga

Ashish donga

Posted on April 19, 2022

DynamoDB:ConditionalCheckFailedException | Status Code: 400; Error Code: ConditionalCheckFailedException

DynamoDB:ConditionalCheckFailedException, Status Code: 400; Error Code: ConditionalCheckFailedException

"message": "The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: HND4VSCK8HLM5NC08VDEV51CORVV4KQNSO5AEMVJF66Q9ASUAAJG)"

quick solution is here

Your goal is to save both records. There are 2 issues here

  1. With your choice of hash and range key it is impossible to save both records

The combination of hash and range key make a record unique. Event 1 and event 2 have the same values for hash and range key. Therefore the second put-item wil simply overwrite the first record.

  1. Your ConditionExpression prevents the replacement of record 1 by record 2

The ConditionExpression is evaluated just before putting a record. Your expression fails because when your event 2 is about to be inserted, DynamoDB discovers that a record with aggregateId “id1” already exists. The condition fails on "attribute_not_exists(aggregateId)", and you receive the ConditionalCheckFailedException This expression prevents overwriting of record 1 by record 2.

If you want to save both records you will have to come up with another choice of hash key and/or range key that better represents the unicity of your data item. You can not solve that with a ConditionExpression.

other solution

An item with that ID allready exists in the Table.

You need to create a unique ID for the item you try to add.
Enter fullscreen mode Exit fullscreen mode

If you have any doubt regarding DynamoDB:ConditionalCheckFailedException then ask in comment

💖 💪 🙅 🚩
ashishdonga
Ashish donga

Posted on April 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related