首页 > 解决方案 > 如何添加/更新 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'
)

我遇到了这些错误,我意识到我在这里做了一些愚蠢的事情:re

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

标签: amazon-dynamodb

解决方案


想通了,我需要为列表添加类型信息:

":my_value": {"L": [{"S": "test"}]}, ":empty_list": {"L": []}

推荐阅读