0

The goal is to update one of the attributes in the DynamoDB table. Here is the schema:

{
  'job_id': {S: jobInfo.job_id} (partition_key),
  'company': {S: jobInfo.company},
  'title': {S: jobInfo.title},
  'posted_on': {S: jobInfo.posted_on},
  'clicks': {N: jobInfo.clicks}

}

The primary key of this table is only partition_key and it is job_id. Now I want to increment the clicks value and here is my update param:

  let params = {
    TableName: 'jobPosts',
    Key: {
        "job_id": "57a08a5c-ca6c-4056-b892-dc9e6388adb9"
    },
    UpdateExpression: "add clicks :value",
    ExpressionAttributeValues: {
        ":value": {N: "1"}
    },
    ReturnValues:"ALL_NEW"
  };

  ddb.updateItem(params, function(err, data) {
    if (err) {
      console.log("In DynamoDB error");
      console.log(`Error: ${err}`);
    } else {
      console.log("In DynamoDB Success");
      console.log(`Here is the data: ${data}`);
    }
  });
};

But, I get the ValidationException error with the message The provided key element does not match the schema. Here is the full stack trace:

{
    "message": "The provided key element does not match the schema",
    "code": "ValidationException",
    "time": "2019-11-17T04:23:11.495Z",
    "requestId": "4327RBE0UEHSND9Q4M7K44AEEFVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 37.586092348332265
}

The table doesn't have a composite primary key. It is just partition_key. I don't understand the issue here.

Lakshman Diwaakar
  • 7,207
  • 6
  • 47
  • 81

1 Answers1

1

When you specify the key, use a format such as this:

Key={"job_id": {"S": "57a08a5c-ca6c-4056-b892-dc9e6388adb9"}}

It is confusing, but when specifying the key you must specify not only the field that is the key but the type of the key. So in this case it is like the "value" for the job_id is a type/value pair, where "S" indicates this is a string type with "57a08a5c-ca6c-4056-b892-dc9e6388adb9" as the value of that string. This shows the format for different types: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html

Shawn
  • 8,374
  • 5
  • 37
  • 60