amazon-dynamodb - 如何添加/更新 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'>
解决方案
想通了,我需要为列表添加类型信息:
":my_value": {"L": [{"S": "test"}]}, ":empty_list": {"L": []}
推荐阅读
- android - 将数据传递给
使用数据绑定布局 - python - RNN模型训练后如何预测以下值?
- java - 如何为 6 英寸 xxhdpi 和 xxxhdpi 设备设置特定布局
- android - .PNG drawable 的 Resources$NotFoundException
- java - 如何从文件中加载图像
- java - 参数化内连接
- c++ - 为什么 vscode 的智能感知找不到我包含的文件?
- gesture - 获取自定义 UIView 以响应两指触控板滚动手势(或鼠标滚轮)
- .net - Blazor WebAssembly + Amazon Cognito
- javascript - 如何在点击时暂停音乐?