How to update a key in dynamodb with boto3?

Viewed 1117

I am new to DynamoDB and am having trouble defining a table. I want to have a table like this:

item_name, create_date, update_date

and I need to query and update the items that have a specific item_name and update_date. So in boto3 I wrote

table.update_item(
            Key={
                'item_name': item_name,
                'update_date': 0
            },
            UpdateExpression='SET update_date = :val1',
            ExpressionAttributeValues={
                ':val1': 999999999
            }
        )

and I get botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute update_date. This attribute is part of the key.

In my table definition, I have item_name as hash key and update_date as range key. How am I going to fix the definition or code so that

  1. The update operation succeeds.
  2. I can quickly query the table on both item_name and update_date attributes?
1 Answers

1)First of all updated_date is part of your primary key means primary key in dynamodb is made of two attribute, Hash(item_name) and Range(updated_date) Primary Key, and you cannot primary key.

2) If you want to update the updated_date property then you should update your table configuration and select item_name only as primary key

Related