20
votes

I created a table 'user_info' in DynamoDB with one primary hash key 'user_id'(String), no range key. Then I created 2 AWS lambda functions to insert and query the items. I can insert items into the table, but when I query the table, it returns:

ValidationException: The provided key element does not match the schema.

My query function :

var params = {
   Key: {
       user_id:{
           S: "[email protected]"
       }
   },
    TableName: 'user_info',
    ProjectionExpression: 'password'
};

dynamodb.getItem(params,
 function(err, data) {
    if (err) {
        console.log("get item err." + err);
        context.done('error','getting item from dynamodb failed: '+err);
    }
    else {
        console.log('great success: '+JSON.stringify(data, null, '  '));
        context.succeed('created user ' + event.user_id + ' successfully.');
    }
});

I keep getting this exception:

ValidationException: The provided key element does not match the schema

Since

1) I have only one hash primary key.

2)user_id is defined as String. I really don't know why there is a mismatch error.

4

4 Answers

48
votes

To clarify further on why this is happening, you were originally using the DynamoDB Document Client, which eliminates to need to explicitly label your attributes as "String" (S) or "Number" (N) etc. Therefore, your original code would have worked with

var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

var params = {
Key: {
   user_id: "[email protected]"
},
TableName: 'user_info',
ProjectionExpression: 'password'
};

Note the "S" wrapping the value of "user_id" is removed from the above code. Later on, you switched back to the low-level javascript sdk by using 'aws-sdk', so your code with "S" label ended up working.

20
votes

At last, I found out the answer. It's not about the format of params, but with the code before it, which I did not post in my question. When I replace

var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

with

var doc = require('aws-sdk');
var dynamodb = new doc.DynamoDB();

the exception disappears.

3
votes

(Different Scenario than OP)

This same error was happening to me when I was trying to query by just a HASH key when my table had both a HASH and SORT Key. I removed the unused SORT key as it wasn't needed for me and it resolved my issue.

0
votes

This error can also be caused by a simple coding error where the querying key's data type is different from the one defined in the schema.

For example, the key is defined as String in the schema (see AttributeDefinitions):

 resources:
 Resources:
   RemarksDynamoDbTable:
     Type: "AWS::DynamoDB::Table"
     DeletionPolicy: Retain
     Properties:
       TableName: "chat-admin-${opt:stage, self:provider.stage}"
       ProvisionedThroughput:
         ReadCapacityUnits: 1
         WriteCapacityUnits: 1
       AttributeDefinitions:
         - AttributeName: id
           AttributeType: S         
       KeySchema:
         - AttributeName: id
           KeyType: HASH 

But it's specified as Number (N) when querying:

result, err := svc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(table),
Key: map[string]*dynamodb.AttributeValue{
    "id": {
        N: aws.String(key),
    },
},})