0
votes

I am updating a counter in dynamodb and it works fine, but I would also like to do an additional update based on the value.

                response=table.update_item(
                    Key={
                        "record_key":record_key_val,
                        "record_type":f"REPROC|{assignment_id}"
                        },
                    UpdateExpression="""
                      SET  #attempt = #attempt + :val,
                           #last_status_code = :last_status_code
                    """,
                    ExpressionAttributeNames={
                        "#attempt": "attempt",
                        "#last_status_code" : "last_status_code"
                    },
                    ExpressionAttributeValues={
                            ":val": 1,
                            ":last_status_code": last_status_code
                    },
                    ReturnValues='UPDATED_NEW',
                )  

BUT I would like to add this logic in the update too

I have attribute "status" with value RETRY and would like to update it to EXPIRE after attempt > 10.

So what I think I need is ExpressionAttributeValues to have conditional logic. Can that be done?

1

1 Answers

0
votes

You wont be able to implement that type of query logic in a single call to update_item.

What you can do is specify a ConditionExpression that prevents your attempt field from being incremented past 10.

table.update_item (
  // your update code 
  ConditionExpession = "#attempt <= 10"
)

Otherwise, you could take advantage of the ReturnValues that come from your update call, which will tell you the new value of the attempt attribute. Your application can conditionally issue another update call to set the status field if the value of attempt equals 10.