![](/img/trans.png)
[英]Boto3 DynamoDB is it possible to do update_item on a list of objects
[英]python3 dynamoDB Update_item do not work
我只是在AWS dynamoDB中練習使用示例代碼但是,更新代碼不能用於錯誤
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: The document path provided in the update expression is invalid for update
我的python代碼在這里。 我可以在沒有'map'屬性的情況下更新DB。
table = dynamodb.Table('Movies')
title = "The Big New Movie"
year = 2015
response = table.update_item(
Key={
"year": year,
"title": title
},
UpdateExpression="set #attrName.rating = :r, #attrName.plot=:p",
ExpressionAttributeNames = {
"#attrName" : "info"
},
ExpressionAttributeValues={
':r': decimal.Decimal(5.5),
':p': "Everything happens all at once."
},
ReturnValues="UPDATED_NEW"
)
發生這種情況是因為您正在嘗試更新頂級屬性info
嵌套屬性,該屬性尚不存在(或者不是地圖類型 )
因此,在運行此更新之前,您必須確保頂級屬性info
已存在。
或者,如果拋出異常,您可以捕獲異常,並執行創建info
屬性的另一個更新,如下所示:
from botocore.exceptions import ClientError
table = dynamodb.Table('Movies')
title = "The Big New Movie"
year = 2015
try:
# Adding new nested attributes `rating` and `plot`
# if the top field `info` already exists and is a map
response = table.update_item(
Key={
"year": year,
"title": title
},
UpdateExpression="set #attrName.rating = :r, #attrName.plot=:p",
ExpressionAttributeNames = {
"#attrName" : "info"
},
ExpressionAttributeValues={
':r': decimal.Decimal(5.5),
':p': "Everything happens all at once."
},
ReturnValues="UPDATED_NEW"
)
except ClientError as e:
if e.response['Error']['Code'] == 'ValidationException':
# Creating new top level attribute `info` (with nested props)
# if the previous query failed
response = table.update_item(
Key={
"year": year,
"title": title
},
UpdateExpression="set #attrName = :attrValue",
ExpressionAttributeNames = {
"#attrName" : "info"
},
ExpressionAttributeValues={
':attrValue': {
'rating': decimal.Decimal(5.5),
'plot': "Everything happens all at once."
}
},
ReturnValues="UPDATED_NEW"
)
else:
raise
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.