0
votes

Code is below

import boto3
dynamodb = boto3.resource ('dynamodb')
table =dynamodb.Table('test')
def lambda_handler(event, context):
    response = table.update_item(
        Key={
            'id': "100",
            'name': "David"
            })

I have created a DynamoDB table test my primary key is id which is string.

in DynamoDB my table value for id 100 is John i need to update to David. Above is the code. Why error is throwing the meta schema

Full error is below

"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: The document path provided in the update expression is invalid for update", "errorType": "ClientError",

Tried below code

import boto3
dynamodb = boto3.resource ('dynamodb')
table =dynamodb.Table('test')
def lambda_handler(event, context):
    response = table.update_item(
    Key={
        'id': '100'
        },
    UpdateExpression='SET name = :val1',
    ExpressionAttributeValues={
        ':val1': 'David'
    })

Adding one more table for replicate the case

TO put the table: Output >> Success

First create table newTable in DynamoDB

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    response = table.put_item(
    Item={
        'username': 'Ac',
        'first_name': 'DEF',
        'last_name': 'FHI',
        'age': 10,
        'account': 'GOld'
    })

How to get the item ? Output >> Error

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    response = table.get_item(
        Key={
            'username':'Ac'
        }
        )
    print (response)

Error >> Response: "errorMessage": "An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema", "errorType": "ClientError",

1
Can you please post the error messag?Marcin
@Marcin "An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema", Client Erroraysh
@Marcin ther is no shortkeyaysh
What is the schema?Marcin
@Marcin both are stringsaysh

1 Answers

1
votes

Answer of second one

get and update need the exact item to be updated not batches, so you also need to provide the corresponding sort key

Courtesy @Sairsreenivas

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource ('dynamodb')
    table =dynamodb.Table('newTable')
    # response = table.put_item(
    # Item={
    #     'username': 'Ac',
    #     'first_name': 'DEF',
    #     'last_name': 'GH',
    #     'age': 10,
    #     'account': 'GOld'
    # })
    # try:
    #     response = table.get_item(Key={'username':'Mak'})
    # except Exception as e:
    #     print(e.response['Error']['Message'])
    # else:
    #     return response['Item']
    # item = response['Item']
    # print (item)
    #Get Item
    response = table.get_item(Key={'username':'Ac', 'last_name':'GH'})
    print (response['Item'])
    table.update_item(
        Key ={
            'username':'Ac', 'last_name':'GH'
        },
        UpdateExpression = 'SET age = :value1',
        ExpressionAttributeValues={
            ':value1':20
        }
        )
    print ("After update \n")
    response = table.get_item(Key={'username':'Ac', 'last_name':'GH'})
    print (response['Item'])