34

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: "usr1@s.com"
       }
   },
    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.

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
Rafy
  • 817
  • 1
  • 7
  • 9

4 Answers4

56

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: "usr1@s.com"
},
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.

Daniela Miao
  • 1,353
  • 7
  • 7
  • 3
    Thank you so much! I always hated DynamoDb's types and wasn't aware of the Document Client (which is now part of the official SDK). Your answer saved me much grief! – Julian Jan 25 '17 at 21:56
  • 1
    Very useful, I was using just new AWS.DynamoDB and I had to include the attribute type, when I switched over to use new AWS.DynamoDB.DocumentClient() I kept getting "ValidationException: The provided key element does not match the schema" without it telling me what was wrong. Leaving out the attribute type fixed the error! – Meir Snyder Jan 24 '19 at 22:52
  • 1
    I ran into the same problem when I tried to make my code as close as possible to AWS code samples, official documentation. When I removed the "S" and "N" and reverted back to the old style, the error disappeared! I went even as far as creating an index and trying all sorts of other things because the error message didn't help in narrowing down on the source! I think it is high time AWS updates the documentation: as far as documentation is considered, consistency isn't the word that I would use for AWS. Thanks a lot Daniela. – R.W Sep 23 '21 at 11:14
  • Thanks, you saved my day. AWS typing is really messy. I used Document Client but tried to initialize DB request using types attributes with "S", "N" notations (BatchWriteItemCommandOutput type). But had to use BatchWriteCommandInput type. – Jeka Developer Jun 20 '22 at 09:14
20

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.

Rafy
  • 817
  • 1
  • 7
  • 9
7

(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.

Fostah
  • 2,947
  • 4
  • 56
  • 78
0

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),
    },
},})
yoges nsamy
  • 1,275
  • 1
  • 16
  • 29