使用python boto3的Dynamodb更新项表达式

问题描述 投票:1回答:2

我有一个字符串字段“title”。我正在尝试使用更新表达式更新它

persontable.update_item(Key={'person_id':person_id}, UpdateExpression="SET title = UPDATED")

我明白了

An error occurred (ValidationException) when calling the UpdateItem operation: The provided expression refers to an attribute that does not exist in the item

我可以在AWS控制台中看到该人的属性“title”。是什么赋予了?

python-2.7 amazon-web-services amazon-dynamodb boto
2个回答
4
投票

不要直接将值插入表达式。而是使用ExpressionAttributeValues - 请参阅boto3 guide

persontable.update_item(Key={'person_id':person_id},
                        UpdateExpression="SET title = :updated",                   
                        ExpressionAttributeValues={':updated': 'UPDATED'})

0
投票

在更新之前检查项目是否已正确创建。

当表未处于“ACTIVE”状态时,如果您尝试put_item - 当您尝试更新未创建的相同项目时,将不会创建它。将发生此错误。

当状态处于“ACTIVE”状态时执行put_item,然后正确更新项目。你不会得到这个错误。

© www.soinside.com 2019 - 2024. All rights reserved.