3

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

1 Answer 1

5

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"}
            }
        )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.