UpdateExpression使用boto3为DynamoDB表中的更新函数获取动态值 [英] UpdateExpression taking dynamic values for update function in DynamoDB table using boto3

查看:301
本文介绍了UpdateExpression使用boto3为DynamoDB表中的更新函数获取动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第一部分的两部分问题 使用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

有人可以帮我吗?

推荐答案

您正在尝试更新该项的密钥,该密钥无效.您将必须删除并重新创建该项目.

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 use PutItem to create a new item with new attributes.

这篇关于UpdateExpression使用boto3为DynamoDB表中的更新函数获取动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆