aws-sdk-js icon indicating copy to clipboard operation
aws-sdk-js copied to clipboard

DynamoDB can Conditional Put ensure uniqueness on non-key attributes?

Open benishak opened this issue 9 years ago • 6 comments

Can I use ConditionalExpression to ensure that a duplicate item is not inserted with a non-key ?

Consider the example I have User table with the following params

- Primary Key : userId,
- Range Key : activityId
- non-keys attributes : username, email, activityCount

Can I use Condition Expression when creating to ensure uniqueness on username or/and email ?

let params : DynamoDB.DocumentClient.PutItemInput = {
            TableName : 'User'
            Item: {
                userId : uid
                username : "benishak",
                email : "[email protected]",
            },
            ExpressionAttributeNames : {
                '#un' : 'username',
                '#email' : 'email',
            },
            ConditionExpression : 'attribute_not_exists(#un) OR attribute_not_exists(#email)',
        }

        db.put(params).promise().then(....);

or

ConditionExpression : '#un <> :un OR #email <> :email',

I tested it and it seems to work but article around the internet seems to contradict me, people say ConditionExpression works only with HASH/Range Keys, so I though maybe I'm doing something wrong ....

benishak avatar Jul 02 '17 18:07 benishak

Hi @benishak,

I don't think you can enforce uniqueness on non-key attributes in this way. According to the developer guide, the attribute_not_exists function does something different when the field is a key vs not a key. For key fields, the operation will not be executed if an item with the provided key already exists in the table; for non-key fields, the operation will not be executed if the item already in the table with the same key as the PutItemInput.Item value has an attribute with the provided name.

You could enforce uniqueness on the client side by creating a global secondary index for each attribute you want to be unique with that attribute as its hash key, then querying that index before inserting a new user record.

jeskew avatar Jul 04 '17 18:07 jeskew

@jeskew what if 2 users are querying that index for uniqueness at the same time? Both users will get the green flag to putItem and you will end up with duplicates for a unique field.

superandrew213 avatar Aug 24 '17 01:08 superandrew213

I wonder if you just create another table with that attribute as hashKey, now you can use condition. and create a dynamo stream to update the main table whenever the helper table succeeded.

linehrr avatar Aug 20 '18 16:08 linehrr

just stumbled upon the similar issue, and makes me think, is dynamo db nuts? There should be conditional checks on non-key attributes also...

artista7 avatar Jun 10 '19 08:06 artista7

Running into this problem myself, I'm thinking the solution would be to use updateItem rather than put, by supplying the field wanted to be kept unique in the key query and using a condition expression to cancel the operation if that field exists. In effect, updateItem should fail if the query gets a result, and succeed if it didn't, creating a new item.

Triforcey avatar Jun 27 '19 20:06 Triforcey

Locking this thread as the issue is old, and resolved. @Triforcey Please open a new issue for related bugs and link to relevant comments in this thread.

trivikr avatar Jun 27 '19 21:06 trivikr