AWS DynamoDB with Python (Boto3) — Part 4— Update Attribute & Delete Item from DynamoDB
If you haven’t read the previous article in this series — AWS DynamoDB with Python (Boto3) — Part 3 — Query Items from DynamoDB
DynamoDB Limitations on Update & Delete
One important constraint we should remember about DynamoDB is
Constraint 1
We must pass the PRIMARY KEY (PARTITION KEY & SORT KEY) while Updating an attribute in an Item (or) Deleting an Item
Ok, why this constraint important ?
Lets consider a simple SQL query
UPDATE STUDENT SET EXAM_CENTRE = 'CHENNAI' WHERE STUDENT_HOME_TOWN = 'CHENNAI';
Above query will update all rows that match the given condition.
But in DynamoDB, we can’t update all items which match a particular condition. Condition given in update query should lead us to updation of one item in DynamoDB.
Same constraint applies for Delete also. Consider another SQL query
DELETE FROM STUDENT WHERE STUDENT_HOME_TOWN = 'CHENNAI'
Above query will delete all rows that match the given condition.
But in DynamoDB, we can’t delete items which match a particular condition. Condition given in delete query should lead us to deletion of one item in DynamoDB.
So what if, we want to update number of rows ? We have to use DynamoDB batch feature to update multiple items. We will discuss batch feature in next article.
Constraint 2
We can’t update the values of the KEYS (PARTITION KEY & SORT KEY) which we pass as part of the update query
We will discuss this with an example later in this article
Update Attribute of an Item
update_item() is used to update non key attribute values in DynamoDB.
def update_student_name(dept, stud_id, stud_name):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table = dynamodb.Table('Student')
response = table.update_item(
Key={
'dept': dept,
'stud_id': stud_id
},
UpdateExpression='SET stud_id = :stud_name',
ExpressionAttributeValues={
':stud_name': stud_name
}
)
print(response)
What if, we try update the stud_id (sort key) in the above code ? Thats when the Constraint 2 comes into picture
def update_student_name(dept, stud_id, stud_name):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table = dynamodb.Table('Student')
response = table.update_item(
Key={
'dept': dept,
'stud_id': stud_id
},
UpdateExpression='SET stud_id = :stud_id',
ExpressionAttributeValues={
':stud_id': stud_id + "random"
}
)
print(response)
Above code will fail with ValidationException
An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute stud_id. This attribute is part of the key
Delete Item from Table
Use delete_item() function to delete an item
def delete_student(dept, stud_id):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table = dynamodb.Table('Student')
delete_item_resp = table.delete_item(
Key={
'dept': dept,
'stud_id': stud_id
}
)
print(delete_item_resp)
Remove Attribute From an Item
We can REMOVE any non key attribute from an item using update_item() function
def drop_student_name(dept, stud_id):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")
table = dynamodb.Table('Student')
response = table.update_item(
Key={
'dept': dept,
'stud_id': stud_id
},
UpdateExpression='REMOVE stud_name'
)
print(response)
We dropped the attribute stud_name for given item
Thats all about Update & Delete in DynamoDB !
Next article in this series — AWS DynamoDB with Python (Boto3) — Part 5 — Bulk Insert & Delete with DynamoDB
GitHub Repo URL — https://github.com/iamvickyav/dynamodb-with-python-boto3