Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

python 3.x - UpdateExpression taking dynamic values for update function in DynamoDB table using boto3

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
    
          )

I am getting an error

 "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",

I know the issue is with how i am handling UpdateExpression

Could any one help me put with this one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You’re trying to update the item’s key, which doesn’t work. You will have to delete and recreate the item.

For details, please refer to the Amazon DynamoDB documentation:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...