'Using DynamoDB transact_write_items how to do a ConditionCheck for an existing item and Put a new item if ConditionCheck is True?
I want to insert a new item in the table only if a particular item already exists. Is it possible to achieve this using transact_write_items? I want to avoid querying the table and then inserting the new item.
response = dynamo_client.transact_write_items(
TransactItems=[
{
'ConditionCheck': {
'Key': {
'indicator_id': {
'S': 'indicator_1'
}
},
'ConditionExpression': 'attribute_exists(#indicator_id)',
'ExpressionAttributeNames': {
'#indicator_id': 'indicator_id'
},
'TableName': 'CAS'
},
'Put': {
'Key': {
'indicator_id': {
'S': 'update_indicator_1'
}
},
'TableName': 'CAS'
}
}
]
)
This throws the following error :
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the TransactWriteItems operation: TransactItems can only contain one of Check, Put, Update or Delete
Solution 1:[1]
The problem was with the syntax.
The correct syntax is :
response = dynamo_client.transact_write_items(
TransactItems=[
{
'ConditionCheck': {
'Key': {
'indicator_id': {
'S': 'indicator_1'
}
},
'ConditionExpression': 'attribute_exists(#indicator_id)',
'ExpressionAttributeNames': {
'#indicator_id': 'indicator_id'
},
'TableName': 'CAS'
}
},
{
'Put': {
'Key': {
'indicator_id': {
'S': 'update_indicator_1'
}
},
'TableName': 'CAS'
}
}
]
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kamaldeep-j |
