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