UpdateExpression 使用 boto3 为 DynamoDB 表中的更新函数获取动态值 [英] UpdateExpression taking dynamic values for update function in DynamoDB table using boto3
问题描述
这是第一部分的两部分问题使用boto3实现的lambda更新DynamoDB表的值功能
This is two part question for first part Updating the value of DynamoDB table with boto3 implemented lambda function
现在我想要实现的是动态更新价值
now what i am trying to achieve is having dynamic value to update
def lambda_handler(event, context):
param = event['queryStringParameters']['employeID']
name = event['queryStringParameters']['employeName']
dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
table = dynamodb.Table('api_demo_employe')
column = [cloumn1,cloumn2......]
for i in range(0,len(column):
query = 'SET {} = :f'.format(column[i])
response = table.update_item(
Key = {
'employeID' : param
},
ExpressionAttributeValues = {
':f': name
},
UpdateExpression = query
)
我遇到了一个错误
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute employeID. This attribute is part of the key",
我知道问题在于我如何处理 UpdateExpression
I know the issue is with how i am handling UpdateExpression
有谁能帮我把这个放上去吗?
Could any one help me put with this one?
推荐答案
您正在尝试更新项目的密钥,但不起作用.您必须删除并重新创建该项目.
You’re trying to update the item’s key, which doesn’t work. You will have to delete and recreate the item.
有关详细信息,请参阅 Amazon DynamoDB 文档:
For details, please refer to the Amazon DynamoDB documentation:
您不能使用 UpdateItem
来更新任何主键属性.相反,您需要删除该项目,然后使用 PutItem
创建具有新属性的新项目.
You cannot use
UpdateItem
to update any primary key attributes. Instead, you will need to delete the item, and then usePutItem
to create a new item with new attributes.
这篇关于UpdateExpression 使用 boto3 为 DynamoDB 表中的更新函数获取动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!