Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB - Key element does not match the schema

I'm trying to update an Item in my Dynamodb Table +Users+. I have tried many different ways but I always received the same error message:

The provided key element does not match the schema

The creation of an Item works, as well as a query but not the update. When I check on DynamoDB the user is well created:

{   "email": "[email protected]",   "password": "123",   "registration": 1460136902241,   "verified": false } 

Here is the table information:

  • Table name: Users
  • Primary partition key: email (String)
  • Primary sort key: registration (Number)

Here is the code (called from lambda):

exports.handler = function(event, context) {     var AWS = require("aws-sdk");       var docClient = new AWS.DynamoDB.DocumentClient();      var params = {         TableName: "Users",         Item:{             email: "[email protected]",             password: "123",             verified: false,             registration: (new Date()).getTime(),         }     };      // Create the user.      docClient.put(params, function(err, data)     {         if (err)         {             context.fail("Put failed...");             return;         }          var params = {             TableName: "Users",             Key: { email : "[email protected]" },             AttributeUpdates: {                 verified: {                     Action: "PUT",                     Value: true                 }             }         };          // Update the user.         docClient.update(params, function(err, data)         {             if (err)             {                 console.log(JSON.stringify(err));                 context.fail(JSON.stringify(err));                 return;             }             context.succeed("User successfully updated.");         });       });  }; 

Do you have any idea of what could be wrong in my code?

like image 660
user6178502 Avatar asked Apr 08 '16 18:04

user6178502


People also ask

Can we change primary key value in DynamoDB?

You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes. The UpdateItem operation includes an Action parameter, which defines how to perform the update. You can put, delete, or add attribute values.

What is DynamoDB range Key?

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

Which one of the following data types does Amazon DynamoDB not support?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type. It can be useful instead to store data and time data as a number data type, using Unix epoch time.

How do I remove the sort key in DynamoDB?

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


1 Answers

You are only providing half of your primary key. Your primary key is a combination of the partition key and range key. You need to include the range key in your Key attribute in the update parameters.

like image 175
Mark B Avatar answered Oct 21 '22 10:10

Mark B