Vinay
Vinay

Reputation: 725

How to add new attributes to a existing row without change existing attributes for Dynamodb Table

I have a table in DynamoDB with following attributes and respective values in a row.

  1. key
  2. attribute1
  3. attribute2
  4. attribute3

Now I want to add attribute5, attribute6, attribute7 to the same row in my table without changing the existing attribute values.

I tried the below code and its deleting old attribute value.

try:
   my_table.put_item(Item={'key': keyvalue,'attribute5': 0, 'attribute6':0, 'attribute7':0})
except Exception as e:
   logger.error("at method add_source_defaults %s", e)

How can i achieve this task.

Upvotes: 1

Views: 2756

Answers (1)

Gulzaman
Gulzaman

Reputation: 429

You need to update your item by getting it with your table hash key and then update it like this.

from boto.dynamodb2.table import Table
my_table = Table('tablename')

item = my_table.get_item(key ='keyvalue')

item['attribute5'] = 0

item['attribute6'] = 1

item['attribute7'] = 2

item.save()

Upvotes: 3

Related Questions