python - 从 DynamoDB 获取项目时出现 "The provided key element does not match the schema"错误

标签 python amazon-dynamodb boto3

这是表分区键设置 enter image description here

表格内容 enter image description here

当我尝试从表中获取项目时,它会打印此错误

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema

这是我的代码

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('testDynamodb')
response = table.get_item(Key={'userId': "user2873"})
item = response['Item']
print(item)

有什么想法吗?谢谢。

最佳答案

您的表架构同时定义了哈希键和排序键。使用 DynamoDB GetItem 时,您必须同时提供它们,这里是 documentation 的摘录

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

鉴于您的示例,get_item 参数应如下所示:

response = table.get_item(Key={'userId': "user2873", 'createdAt': "1489376547"})

关于python - 从 DynamoDB 获取项目时出现 "The provided key element does not match the schema"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757872/

相关文章:

python - Jupyter 笔记本无法从 Centos 7 打开

php - 在 Python 中使用 POST 将数据发送到 PHP

python - 遍历字典并以特定格式打印

amazon-web-services - 独立游戏后端的 Aurora 与 Redshift 与 DynamoDB?

python - 意外删除后如何在 ubuntu 中恢复 python 2.7[弄乱了我的/usr/bin/]

database-design - DynamoDB 单表设计中的分区键或排序键是否应该仅包含不太可能更改的字段?

javascript - DynamoDB |如何向现有对象添加 bool 值

python - 如何在 Django 中将 S3 文件附加到电子邮件

amazon-web-services - 为什么 boto3 s3 对象缺少存储类属性?

aws-cloudformation - boto3 cloudformation 参数验证失败