DynamoDB - Key element does not match the schema

Amazon Web-ServicesAmazon Dynamodb

Amazon Web-Services Problem Overview


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?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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.

Solution 2 - Amazon Web-Services

For others who have faced the same challenge and the issue is not fixed by above answers, it is always better to double check the data type of the value being updated, in my case the primary key was expecting a Number and I was trying to update with a string. Silly me

Solution 3 - Amazon Web-Services

My checklist when facing this issue:

  1. Check that the name and type of your key correspond to what you have in the database.
  2. Use corresponding attributes to make it explicit. E.g. use @DynamoDBHashKey(attributeName = "userId") in Java to indicate the partition key named userId.
  3. Ensure that only one field or getter marked as partition key in your class.

Please, add more if you know in the comments.

Solution 4 - Amazon Web-Services

I was doing BatchGetItem, then streamed it to BatchWriteItem (Delete). DeleteItem didn't like it got all attributes from the object instead of only partition and sort key.

Gathering all answers:

  • mismatch in an attribute name
  • mismatch in attribute type
  • half key provided
  • unnecessary additional keys

Solution 5 - Amazon Web-Services

My issue was with the Node SDK for deletes, where the documentation says to provide in format:

... {Key: {'id': {S: '123'}}} ...

Which does not appear to work with the aws-sdk ^2.1077.0. This seems to work:

... {Key: {'id': '123'}} ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6178502View Question on Stackoverflow
Solution 1 - Amazon Web-ServicesMark BView Answer on Stackoverflow
Solution 2 - Amazon Web-Servicesharishanth raveendrenView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesSergei RusskikhView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesnouveuView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesplantbeardView Answer on Stackoverflow