简体   繁体   English

dynamoDB update_item 方法 - 关键元素不匹配架构错误只是不 go。 尝试了一切

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

I have tried all methods mentioned in many different docs of aws dynamoDB.我已经尝试过许多不同的 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:我在 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.我在这里想念什么。

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().最初的问题是,我使用 boto3.resource() 启动了 boto3 object 作为资源,但指的是 boto3.client() 的 aws 文档。

If you're using a resource, the update method is easier: this is documentation link for boto3 resource如果您使用的是资源,更新方法更简单: 这是 boto3 资源的文档链接

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但是,如果您使用的是客户端,则必须更改语法: 这是 boto3 客户端的文档链接

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.希望现在没有人感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM