Boto3 Dynamodb list_append Invalid type for parameter ExpressionAttributeNames type: <class 'set'>, valid types: <class 'dict'>

Question:

Error message is throwing me. I have update_item updating an existing list. When I put everything in the UpdateExpression, it works fine. When I use the equivalent with ExpressionAttributeNames, I get the error in the title. They look equivalent to me, but hopefully someone can point out my error.

This works fine:

update_resp = table.update_item(Key={
            'client': client,
            'run_date': today
        },
            UpdateExpression=f"SET results.{test_id}.trial_area = list_append(results.{test_id}.trial_area, :r)",
            ExpressionAttributeValues={':r': prior_test_results})

This gives the error:

update_resp = table.update_item(Key={
            'client': client,
            'run_date': today
        },
            UpdateExpression=f"SET #res = list_append(#res, :r)",
            ExpressionAttributeNames= {'#res', f'results.{test_id}.trial_area'},
            ExpressionAttributeValues={':r': prior_test_results})
Asked By: Couch

||

Answers:

DynamoDB interprets a dot in an expression attribute name as a literal character within an attribute’s name, rather than as a hierarchical separator. See the Nested Attributes documentation.

You need to indicate multiple expression attribute name parts, for example

"#res": "results",
"#test": test_id,
"#trial": "trial_area"

and then use #res.#test.#trial in your expression.

Arguably this offsets any advantage you were hoping to get by making the expression shorter, but it should at least solve the problem of hierarchical naming.

Answered By: jarmod