AttributeError: 'Table' object has no attribute 'update_item' - DynamoDB v2 API

6.5k views Asked by At

I am trying to conditionally update an item in DynamoDB using the following code:

from boto.dynamodb2.table import Table

conn = get_layer1_ddb_connection()
values_table = Table(table_name, connection=conn)
attrs = { 'values' : new_values,
          'version' : existing_item['version'] + 1}
condition_expression = 'version = :v'
values_table.update_item(table_name, key=customer_id, attribute_updates=attrs, condition_expression=condition_expression, expression_attribute_values={':v': existing_item['version'],}, return_values='ALL_OLD',)

where, layer1 connection is created like this:

from boto.dynamodb2.layer1 import DynamoDBConnection

def get_layer1_ddb_connection(self):
    return DynamoDBConnection(region=self.region, aws_access_key_id=self.creds[CRED_ACCESS_KEY], aws_secret_access_key=self.creds[CRED_SECRET_KEY])

self.region is of type RegionInfo and self.creds have always worked perfectly for other high level API calls.

2

There are 2 answers

0
user1071840 On

This is what worked for me:

ddb_conn = dynamodb2.connect_to_region(region, self.creds[CRED_ACCESS_KEY], self.creds[CRED_SECRET_KEY], self.creds[CRED_SECURITY_TOKEN])

values_table = Table(table_name, connection=ddb_conn)
key = {"id": {"S": customer_id }}
update_expression = "SET #cv = :new_values, version = :new_version"
condition_expression = "version = :existing_version"
expression_attribute_names = {"#cv" : "customer-values"}

existing_item = values_table.get_item(id=customer_id)
existing_version = existing_item['version']
new_version = existing_version + 1

expression_attribute_values = {":new_values": {"N": str(new_values)}, ":new_version": {"N": str(new_version)}, ":existing_version": {"N": str(existing_version)}}  

ddb_conn.update_item(table_name, key, update_expression=update_expression, condition_expression=condition_expression, expression_attribute_names=expression_attribute_names, expression_attribute_values=expression_attribute_values)
0
stevieb On

If I'm not mistaken, this may be a hint in the source of table.py. Inside the private _update_item() method, it actually calls conn.update_item.

self.connection.update_item(self.table_name, raw_key, item_data, **kwargs)

So perhaps try calling update_item() on the connection instead of the table and see if that helps.