Search code examples
pythonamazon-dynamodbboto3botocore

Example of update_item in dynamodb boto3


Following the documentation, I'm trying to create an update statement that will update or add if not exists only one attribute in a dynamodb table.

I'm trying this

response = table.update_item(
    Key={'ReleaseNumber': '1.0.179'},
    UpdateExpression='SET',
    ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')',
    ExpressionAttributeNames={'attr1': 'val1'},
    ExpressionAttributeValues={'val1': 'false'}
)

The error I'm getting is:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "attr1"

If anyone has done anything similar to what I'm trying to achieve please share example.


Solution

  • Found working example here, very important to list as Keys all the indexes of the table, this will require additional query before update, but it works.

    response = table.update_item(
        Key={
            'ReleaseNumber': releaseNumber,
            'Timestamp': result[0]['Timestamp']
        },
        UpdateExpression="set Sanity = :r",
        ExpressionAttributeValues={
            ':r': 'false',
        },
        ReturnValues="UPDATED_NEW"
    )