问题
I have a string field, "title". I am trying to update it with the update expression with
persontable.update_item(Key={'person_id':person_id}, UpdateExpression="SET title = UPDATED")
and I get
An error occurred (ValidationException) when calling the UpdateItem operation: The provided expression refers to an attribute that does not exist in the item
I can see the attribute "title" for that person in the AWS console. What gives?
回答1:
Don't plug the value into the expression directly. Rather use ExpressionAttributeValues -- see boto3 guide
persontable.update_item(Key={'person_id':person_id},
UpdateExpression="SET title = :updated",
ExpressionAttributeValues={':updated': 'UPDATED'})
回答2:
Check whether item has created properly before updating.
When the Table is not in "ACTIVE" state, if you try to put_item - it will not be created and next when when you try to update the same item which is not created. This error will occur.
Execute the put_item when the state is in "ACTIVE" state and then update item properly. You will not get this error.
来源:https://stackoverflow.com/questions/34689912/dynamodb-update-item-expression-with-python-boto3