如何添加/更新DynamoDB条目,创建或附加到列表属性?

时间:2018-04-29 21:28:28

标签: amazon-dynamodb

这是我的用例,我需要在DynamoDB表中添加和更新条目,如下所示:

如果分区键不存在,请添加它并添加一个包含字符串值的新元素列表。 如果分区键确实存在,请将新字符串添加到其列表属性中。 我有Python / boto3的代码如下:

response = ddb_client.update_item(
    TableName=target_ddb_table,
    Key={'email-hash': {'S' : encrypted_hashed_pw},},
    ExpressionAttributeValues={ ":my_value":[{"S":"test"}], ":empty_list":[] },
    UpdateExpression='SET site_ids = list_append(if_not_exists(site_ids, :empty_list), :my_value)',
    ReturnValues='ALL_NEW'
)

我收到这些错误,我意识到我在这里做了一些蠢事:重新

Invalid type for parameter ExpressionAttributeValues.:empty_list, value: [], type: <class 'list'>, valid types: <class 'dict'>
Invalid type for parameter ExpressionAttributeValues.:my_value, value: [{'S': 'test'}], type: <class 'list'>, valid types: <class 'dict'>

1 个答案:

答案 0 :(得分:2)

想出来,我需要为列表添加类型信息:

":my_value": {"L": [{"S": "test"}]}, ":empty_list": {"L": []}
相关问题