Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting errors while trying to update column in DynamoDb using python client

I have a dynamoDB table with the key name as id and a String field called state. I just want to update the value of the state using update_item DynamoDb Python client.

DDB_CLIENT.update_item(
            Key={
                    'id' : {'S': id}
                },
            TableName='TrackingState',
            UpdateExpression="set state = :r",
            ExpressionAttributeValues={
                ':r': '"state": {"S": "IN_PROGRESS"}'
            }
        )

I get an error: Invalid type for parameter ExpressionAttributeValues type: <class 'str'>, valid types: <class 'dict'>

If I try expressionAttributeValues as:

':r' : {"state": {"S": "IN_PROGRESS"}} I get the error: Unknown parameter in ExpressionAttributeValues.:r: "state", must be one of: S, N, B, SS, NS, BS, M, L, NULL, BOOL

If I try

':r' : {"S": "QUEUED"}
Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: state

What is the right way to update an entry in DynamoDb table

like image 281
user1692342 Avatar asked Apr 21 '26 23:04

user1692342


1 Answers

If I try

':r' : {"S": "QUEUED"} Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: state

That's right, the state keyword could not be used in expression, so the ExpressionAttributeNames dict permits to use it.

DDB_CLIENT.update_item(
            Key={
                    'id' : {'S': id}
                },
            TableName='TrackingState',
            UpdateExpression="set #s = :r",
            ExpressionAttributeNames={
                '#s': "state"
            },
            ExpressionAttributeValues={
                ':r': {"S": "QUEUED"}
            }
        )
like image 57
Christophe Morio Avatar answered Apr 24 '26 19:04

Christophe Morio