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.
A DynamoDB table's Sort Key and Partition Key definitions cannot be changed. (You can update an individual record's PK and SK values, but you cannot change the PK or SK setting.) It is a typical pattern to use the generic key names PK and SK , for this reason (and that the pattern also enables a single-table design).
Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results. Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value.
The sort key of an item is also known as its range attribute. The term range attribute derives from the way DynamoDB stores items with the same partition key physically close together, in sorted order by the sort key value. Each primary key attribute must be a scalar (meaning that it can hold only a single value).
DynamoDB is a key-value and document database that does not enforce a schema for your data. You can store data items where each item may have different attributes and attribute types. Item values may be primitive values, scalars or compound documents.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With