1

I have a dynamodb table, with columns as id (partition key) and date. I am trying to update the date where id=2, but I am getting below error as response:

message: "The provided key element does not match the schema"
__type: "com.amazon.coral.validate#ValidationException"

Below is my code:

import * as AWS from 'aws-sdk'
AWS.config.update({
    region: 'us-east-1',
    accessKeyId: 'MY_ACCESS_KEY',
    secretAccessKey: 'MY_SECRET_KEY'
});
const docClient = new AWS.DynamoDB.DocumentClient()

export const updateData = (tableName : any, id : any, date : any) => {
        let params = {
            TableName: tableName,
            Key:{
                "id": id
            },
            UpdateExpression: `set date = :date`,
            ExpressionAttributeValues: {":date": date},
        };
        docClient.update(params, function(err, data) {
            if (!err) {
                console.log(data);
            }else{
                console.log(err)
            }
        })
    }

I am calling function like this:

updateData("mytable","2","2023-01-10")

I anyone confirm what I am doing wrong here?

3
  • What's the type of the id key the DynamoDB table? String or Number? Commented Jan 18, 2023 at 8:03
  • Type of id is string Commented Jan 18, 2023 at 8:21
  • Does your table have a sort key? Commented Jan 18, 2023 at 11:36

1 Answer 1

2

date is a reserved keyword in DynamoDB

You need to use ExpressionAttributeNames param:

        let params = {
            TableName: tableName,
            Key:{
                "id": id
            },
            UpdateExpression: `set #date = :date`,
            ExpressionAttributeValues: {":date": date},
            ExpressionAttributeNames: {"#date":"date"}
        };

As for the exception, ensure that your tables partition key is id and if type String, and that the table does not have a sort key, otherwise you need to add the sort key also.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Lee, I was facing issue because of the sort key. Now it's resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.