dynamoDB update_item method - key element does not match schema error just doesnt go. Tried everything

Viewed 1245

I have tried all methods mentioned in many different docs of aws dynamoDB. like this and this.

Also referred to this post.

But it just doesn't seem to work.

I have this entry in dynamoDB:

dict1={
    'token':1234567890,
    'name':'bigCompany',
    'nestedDict':{
        'a':3810,
        'b':3815,
        'c':3805,
        'd':3811
        },
    'High1':3815,
    'Low1':3805
}

And i'm using this code to update an attribute:

updater = table.update_item(
    Key={
        'token': '1234567890',
        'name':'bigCompany'
    },
    ExpressionAttributeNames={'#LM':'Low1'},
    ExpressionAttributeValues={
        ':nlm': {
            'N':'3802'
            }
    },
    ReturnValues='UPDATED_OLD',
    UpdateExpression='SET #LM = :nlm',
)

This is the stack trace:

Traceback (most recent call last):
  File "updating1.py", line 89, in <module>
    UpdateExpression='SET #LM = :nlm',
  File ".local/lib/python3.7/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File ".local/lib/python3.7/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(*args, **params)
  File ".local/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File ".local/lib/python3.7/site-packages/botocore/client.py", line 676, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema

What am i missing here.

1 Answers

Based on the pointers provided by good people in comments, i was able to resolve this so updating both methods.

The issue originally was, i had initiated a boto3 object as a resource using boto3.resource(), however was referring to aws documentation of boto3.client().

If you're using a resource, the update method is easier: this is documentation link for boto3 resource

changer = table.update_item(
    Key={
        'token': '1234567890',
        'name':'bigCompany'
    },
    UpdateExpression='SET LM = :val1',
    ExpressionAttributeValues={
        ':val1': 3802
    }
)

However, if you're using a client, then you'll have to change the syntax: this is the documentation link for boto3 client

changer = client.update_item(
    Key={
        'token': {
            'N':'1234567890',
            },
        'company': {
            'S':'bigCompany'
            },
    },
    ExpressionAttributeNames={'#LM':'val1'},
    ExpressionAttributeValues={
        ':val1': {
            'N':'3802',
        },
    },
    ReturnValues='UPDATED_OLD',
    UpdateExpression='SET #LM = :val1',
    TableName='myTable'
)

Hope no one get's confused now.

Related