0

Is there way to insert item with the same PK without overwriting them? I keep using PutItemRequest but It keeps updating the attributes with that PK.

            {
                TableName = "MovieRating",
                Item = new Dictionary<string, AttributeValue>
                {
                    { "User",  new AttributeValue { S = movieRating.User } },
                    { "Comment",  new AttributeValue { S = movieRating.Comment } },
                    { "MovieTitle",  new AttributeValue { N = movieRating.MovieTitle.ToString() } },
                    { "Rate",  new AttributeValue { N = movieRating.Rate.ToString() } }
                },
                ConditionExpression = "attribute_exists(pk)",
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>
                {
                    { "User" , new AttributeValue {S = movieRating.User}}
                }
            };```
0

1 Answer 1

2

This is expected behavior of PutItem for PrimaryKey

Please Refer https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

which states as below,

Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item. You can perform a conditional put operation (add a new item if one with the specified primary key doesn't exist), or replace an existing item if it has certain attribute values.

I'm not sure what you are trying to achieve here by trying to insert duplicate PrimaryKey, not a valid scenario.

Instead you can have composite primary key, for example in your case you can have MovieTitle + Year as composite primary key, if you are trying to insert multiple records with same MovieTitle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.