"The provided key element does not match the schema" error when getting an item from DynamoDB

PythonAmazon DynamodbBoto3

Python Problem Overview


This is the table partition key setting enter image description here

The table content enter image description here

When I tried to get an item from the table, it prints this error

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

This is my code

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

Any ideas? thanks.

Python Solutions


Solution 1 - Python

Your table schema has both hash key and sort key defined. When using DynamoDB GetItem you must provide both of them, here is an excerpt from 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.

So given your example, here is how get_item parameters should look like:

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

Solution 2 - Python

One other thing that works is the following code below:

from boto3.dynamodb.conditions import Key

result = table.query(
        KeyConditionExpression=Key('userId').eq('user2873')
    )

Solution 3 - Python

I have also got the same issue. The solution to this is :

While creating the Table, define only usrID in your schema as Hash Key. [I mean Single Primary Key]

Then you can call get item based on your usrID. If you are defining usrId and createdAt in your Table Schema as Hash and Sort Key.[I mean composite primary key] You have to provide both while calling getItem.

Solution 4 - Python

I guess you don't have to put all the related attributes

in my case I only have one PK as col

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();

exports.handler = (event, context, callback) => {
  const connectionId = event.requestContext.connectionId;
  deleteConnectionId(connectionId).then(() => {
    callback(null, { statusCode: 200 , message: 'userId deleted'});
  });
};
 
 
function deleteConnectionId(connectionId) {
  return ddb
    .delete({ TableName: 'your table name', 
        Key: {
            userId : connectionId.toString() // userId is my PK in this case
         }
    } )
    .promise();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKeorosView Question on Stackoverflow
Solution 1 - PythonxtxView Answer on Stackoverflow
Solution 2 - Pythonuser754036View Answer on Stackoverflow
Solution 3 - Pythonbhavuk bhardwajView Answer on Stackoverflow
Solution 4 - PythonHasoun GHView Answer on Stackoverflow