是否可以在 update_item 中结合 if_not_exists 和 list_append [英] Is it possible to combine if_not_exists and list_append in update_item
问题描述
我正在尝试在 boto3 中为 DynamoDB 使用 update_item
功能.
I'm trying to use the update_item
functionality for DynamoDB in boto3.
我现在正在努力更新项目列表.如果列表尚不存在,我想创建一个新列表,否则将附加到现有列表.
I'm struggling right now to update lists for items. I would like to create a new list if the list does not exist yet and otherwise append to the existing list.
使用 SET my_list = list_append(my_list, :my_value)
形式的 UpdateExpression
返回错误 "提供的表达式引用了一个属性如果列表尚不存在,则不存在于项目".
Using an UpdateExpression
of the form SET my_list = list_append(my_list, :my_value)
returns an error "The provided expression refers to an attribute that does not exist in the item" if the list does not exist yet.
知道如何修改我的 UpdateExpression
吗?
Any idea how I would have to modify my UpdateExpression
?
推荐答案
您可以使用 list_append(if_not_exists())
构造.
更新表达式:
'SET my_list2 = list_append(if_not_exists(my_list2, :empty_list), :my_value)'
表达式属性值:
{ ":my_value":{"L": [{"S":"test"}]}, ":empty_list":{"L":[]} }
这篇关于是否可以在 update_item 中结合 if_not_exists 和 list_append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!