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}}
                }
            };```
Maulik
  • 510
  • 1
  • 7
  • 22
Nooty
  • 11
  • 3

1 Answers1

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

Maulik
  • 510
  • 1
  • 7
  • 22