- Newest
- Most votes
- Most comments
This is most likely due to the fact that data
dict does not exist in the current item you're trying to update. You cannot update status
if it's within a dict that does not exist, you can catch the exception and add the entire dict which should provide a workaround.
status = order_data['status'] orderId = order_data['orderId'] userId = order_data['userId'] key={ "userId": userId, "orderId": orderId } update_expression = "SET #data.#status = :status" expression_attribute_values = {":status": status} expression_attribute_names = {"#data": "data", "#status": "status"} try: response = ddbTable.update_item( Key=key, UpdateExpression=update_expression, ExpressionAttributeValues=expression_attribute_values, ExpressionAttributeNames=expression_attribute_names ) except ClientError as e: if e.response['Error']['Code'] == 'ValidationException': expression_attribute_values = {":data": {"data":{ "status":status }}} expression_attribute_names = {"#data": "data"} update_expression = "SET #data = :data" response = ddbTable.update_item( Key=key, UpdateExpression=update_expression, ExpressionAttributeValues=expression_attribute_values, ExpressionAttributeNames=expression_attribute_names )
Ensure that your update expression is correctly formatted
import boto3
dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('YourTableName')
def lambda_handler(event, context): response = table.update_item( Key={ 'PrimaryKey': event['PrimaryKey'] }, UpdateExpression="SET attribute1 = :val1, attribute2 = :val2", ExpressionAttributeValues={ ':val1': event['value1'], ':val2': event['value2'] } ) return response
Thanks for replying. From the error, seems the problem is with the SET expression. Due to the table schema naming in the example, the path to the nested attribute should be "data.status". Since both "data" and "status" are special words, they has to be substitute with the # prefix. So I'm lost about why this won't pass the validation.. P.s. I've pasted the actual handler code in the updated question.
Relevant content
- asked 2 years ago
- Accepted Answerasked 3 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 3 years ago
That's not the case though. I've checked that both 'data' dict and the data['status'] attribute are present. Yet it's still complaining.
I tried your suggestion, and it results in something stranger. DynamoDB created a separate item in that table, with the same Hashkey (userId) and default value for rangeKey ( orderId is 1), and the new item simply containing the actual data payload specified in the command.
Just curious.. Maybe it's simply a edge case with the attribute names being reserved words?
The behaviour is not strange, it confirms your issue is that you think the
key
contains different values than what it actually should. Hard-code the keys of the items you expect to target.You are right! Apparently a wrong range key was used during my testing, so it was trying to update a non-existing item. Much appreciate for the help!