69

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.

Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
Keoros
  • 1,337
  • 2
  • 13
  • 23

4 Answers4

132

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"})
xtx
  • 4,306
  • 4
  • 28
  • 30
  • 2
    'Your table schema has both hash key and partition key defined'—I kinda want to tag that with 'citation needed' . Can you explain how you can tell that from the code and image in the question? – Michael Scheper Apr 27 '18 at 22:11
  • 3
    @MichaelScheper Actually, the `hash key` and `partition key` are the same thing. Funny that no one noticed that before. So it should say 'Your table schema has both hash key and sort key defined.' Will fix this. – xtx Apr 28 '18 at 02:19
  • 2
    @MichaelScheper As for your question, you can tell that the schema has both partition (hash) key and sort key defined by looking at the first image (more precisely, at the `Primary partition key` and the `Primary sort key` fields) – xtx Apr 28 '18 at 02:25
  • Yep, now that you've made that correction (and now that I've played a bit more with Dynamo and gained slightly more experience ☺), it makes sense. Thanks! – Michael Scheper Apr 30 '18 at 16:44
  • 1
    does this mean that is impossible to query this table for all records which were createdAt a single value? (all user's created on a certain date?) That seems odd. (noob to dynamodb) – Jason Michael May 19 '18 at 16:52
  • 1
    @JasonMichael you can't do this with GetItem, you'll need to use Scan (https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html) Note that scan operation implies reading all records in a table, so it may work as a single time use, but if your app needs this more than once, consider adapting your table schema. – xtx May 21 '18 at 07:55
37

One other thing that works is the following code below:

from boto3.dynamodb.conditions import Key

result = table.query(
        KeyConditionExpression=Key('userId').eq('user2873')
    )
Alex R
  • 11,364
  • 15
  • 100
  • 180
user754036
  • 537
  • 6
  • 8
4

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.

astqx
  • 2,058
  • 1
  • 10
  • 21
bhavuk bhardwaj
  • 265
  • 1
  • 7
-2

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();
}
Hasoun GH
  • 43
  • 2
  • 6