Putnik
Putnik

Reputation: 6854

DynamodDB: How to update sort key?

The table has two keys: filename (partition key) and eventTime (sort key). I want to update eventTime for certain filename. Tried put_item() and update_item() sending the same filename with new eventTime but those functions add a new item instead of update.

What should I use for that purpose?

Upvotes: 11

Views: 37958

Answers (3)

GorkemHalulu
GorkemHalulu

Reputation: 3075

According to DynamoDB/AttributeValueUpdate aws docs:

You cannot use UpdateItem to update any primary key attributes. Instead, you will need to delete the item, and then use PutItem to create a new item with new attributes.

Upvotes: 9

ravi kiran mahankali
ravi kiran mahankali

Reputation: 134

In DynamoDB, you can perform a conditional update on an existing item(insert a new attribute name-value pair if it doesn't exist, or replace an existing name-value pair if it has certain expected attribute values). Refer to the official documentation here: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html

Also, you may consider using sort keys the right way. Please refer to the suggested documentation here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html

Upvotes: -2

Stargazer
Stargazer

Reputation: 1530

You can't. Since it is part of the index, the field is protected. What you should do it to delete the item and add it again with the new sort key.

See https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValueUpdate.html

Upvotes: 33

Related Questions