DynamoDB ConditionalExpression not recognizing Attribute Names or Values

Question:

I’m using NodeJS’s aws-sdk and am trying to do an update such that if the item doesn’t exist it will throw an error. I’m using the Expression API instead of the legacy one. Here is my contrived example that isn’t working for me.

ValidationException: Value provided in ExpressionAttributeNames unused in expressions: keys: {#idKey}

I’ve tried various other ConditionalExpressions both using attribute names and inserting the actual value into the expression. I’m beginning to think this is a bug. Using the legacy Expected->Exists with the legacy AttributeUpdate works but I am unable to demonstrate this feature with Expressions.

Answer:

You are already narrowing down to the specific item where _id=10 with the Key parameter of your UpdateItemRequest. If an item does not exist, you cannot condition the UpdateItem call on a specific value of a key. Therefore, only the attribute_exists(#idKey) in the ConditionExpression is necessary.

The following code elicits the behavior you desire (I had to change table name to Images and primary key to Id to match the contents of the DynamoDB Local Shell tutorial.

As a reminder, please do not post any real password data here 🙂

Leave a Reply