After generating a random uuid
value I go ahead and assign it to the item.uuid
:
let uuid = uuidv4();
let title = "Title 100"
let item = {
uuid: uuid,
title: title
};
With the Dynamodb table where the Primary Key is the item.uuid
attribute, I add the item to the table with:
let TABLE_NAME = "My Table Name";
let options = {};
let promise = dbClient.put_item({ Item: item, TableName: TABLE_NAME, ...options });
The new item is inserted into the Dynamodb table with the Primary Key uuid
generated earlier.
Next, I want to add another item to the same table, with the same "title 101" title:
let uuid = uuidv4();
let title = "title 101"
let item = {
uuid: uuid,
title: title
};
( both items will have the same title
, but different uuid
used as a Primary Key in a table).
Since I want to make sure, this item will be added only if no other items have the same "title 101" title, I use the ConditionExpression
:
let options = {
ConditionExpression: "#title <> :title",
ExpressionAttributeNames: {
"#title": "title"
},
ExpressionAttributeValues: {
":title" : "title 101"
}
}
let promise = dbClient.put_item({ Item: item, TableName: TABLE_NAME, ...options });
promise
.then(res => {
console.log('--------- SUCCESS --------');
console.log(res);
console.log('-----------------');
});
But regardless of the ConditionExpression
used here, the DynamoDB goes ahead and adds a new item with the same title
value.
Is there a way to use the conditional expression on an attribute (field) that is not a Primary Key?
question from:
https://stackoverflow.com/questions/65894824/how-to-put-the-item-in-dynamodb-with-conditional-expression-that-is-not-primary 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…