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