尝试使用 python 客户端更新 DynamoDb 中的列时出错

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

我有一个 dynamoDB table,键名为 id,字符串字段名为 state。我只想使用 update_item DynamoDb Python 客户端更新状态值。

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

我得到一个错误:Invalid type for parameter ExpressionAttributeValues type: <class 'str'>, valid types: <class 'dict'>

如果我将 expressionAttributeValues 尝试为:

':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

如果我尝试

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

在 DynamoDb 中更新条目的正确方法是什么 table

If I try

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

对了,state关键字不能用在expression中,所以ExpressionAttributeNames dict允许使用

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