Adding new attribute to DynamDB table with updateItem

╄→尐↘猪︶ㄣ 提交于 2021-01-05 11:26:31

问题


I have table already and I want to add a new attribute to that table. I am trying to do that with the update_item functionality of dynamDB.

use case: Bid table holds details on the bid of the product. The user accepts the bid, once the bid is accepted, have to add a few attributes to that record like the user information. Not sure if it is the right way or should I have a new table for this.

pratition key is : Pickup, sort key is : DropOff

A Demo example that I am trying currently currently trying to alter the same table and facing the error.

import json
import boto3

def lambda_handler(event, context):
    
    dynamo_client = boto3.resource('dynamodb')
    users = dynamo_client.Table('LoadsandBids')
 
item = event['body']
print("Fuirst") 
users.update_item(
    Key={
        'Pickup': event['body']['Pickup'],
        'DropOff' : event['body']['DropOff']
                
        },
    UpdateExpression='SET #attr1 = :val1',
    ExpressionAttributeNames={'#attr1': 'new_field'},
    ExpressionAttributeValues={':val1': event['body']['new']},
    ReturnValues='UPDATED_NEW'
)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

getting an error:

"An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema",

Could anyone help me out of this and also suggest it I my approach is good or not?

来源:https://stackoverflow.com/questions/64873480/adding-new-attribute-to-dynamdb-table-with-updateitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!