turbofood
turbofood

Reputation: 312

DynamoDB Unique list of elements across all records

I have a simple table that stores list of names for a particular record. I want to ensure that a name can never be used for any other record more than once. The names column should also not be empty; there should always be at least 1 given name.

|          ID           |          Names                                 |
|-----------------------|------------------------------------------------|
|          111          |          [john, bob]                           |
|          222          |          [tim]                                 |
|          333          |          [bob] (invalid bob already used)      |

Upvotes: 0

Views: 459

Answers (2)

turbofood
turbofood

Reputation: 312

Easiest solution I believe for this case is to simply use a 2nd table for the values you are interested in being the primary key. Then in application code, simply check that new table if they exist or not to determine if you should create a new record in the primary table. For a List[L] this simply avoids having to traverse every single list of every record to determine if a particular scalar value already exists or not. Credit to Tamas.

enter image description here

https://advancedweb.hu/how-to-properly-implement-unique-constraints-in-dynamodb/

Upvotes: 1

hunterhacker
hunterhacker

Reputation: 7132

Here’s a blog post describing how to best enforce uniqueness constraints: https://aws.amazon.com/blogs/database/simulating-amazon-dynamodb-unique-constraints-using-transactions/

Upvotes: 0

Related Questions