bartaelterman
bartaelterman

Reputation: 903

boto DynamoDB: How can I prevent overwriting items with batch_write?

I'm using boto's DynamoDB v2 and I'm writing items to a table in batch. However I'm unable to prevent DynamoDB from overwriting attributes of existing items. I'd rather have the process fail.

The table has the following schema:

from boto.dynamodb2.table import Table, HashKey, RangeKey

conn = get_connection()
t = Table.create(
        'intervals',
        schema=[
            HashKey('id'),
            RangeKey('start')
        ],
        connection=conn
    )

Say I insert one item:

item = {
    'id': '4920',
    'start': '20',
    'stop': '40'
}
t.put_item(data=item)

Now, when I insert new items with batch_write, I want to make sure DynamoDB will not overwrite the existing item. According to the documentation, this should be achieved with the overwrite parameter from the put_item method of the BatchTable class (which is the one that is used as context manager in the example below)

new_items = [{
    'id': '4920',
    'start': '20',
    'stop': '90'
}]

with t.batch_write() as batch:
    for i in new_items:
        batch.put_item(data=i, overwrite=False)

However, it doesn't. The stop attribute in my example gets a new value 90. So the previous value (40) is overwritten.

If I use the table's own put_item method, the overwrite parameter works. Setting it to True replaces the stop value while setting it to False results in a ConditionalCheckFailedException.

How can I get that exception when using batch_write?

Upvotes: 3

Views: 2659

Answers (1)

garnaat
garnaat

Reputation: 45856

I don't think there is any way to do this with DynamoDB. The batch API does not support it. It is a bug in boto that the put_item method of the BatchTable object accepts the overwrite parameter. If you check the code, you can see that it does nothing with that parameter. It is ignored because there is nothing that it can do with it. DynamoDB just doesn't support this. At least not yet.

Upvotes: 3

Related Questions