Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to enforce unique constraint on a property (field) other than the primary key in dynamodb

In dynamodb, if you want to enforce uniqueness in a field other than the primary key (like were you have a users table and want unique email addresses for users while primary key is a userid which is a number) is there a way other thans scanning the table to see if the email is already in use?

like image 796
Ali Avatar asked Oct 16 '12 18:10

Ali


People also ask

How do you make a field unique in DynamoDB?

In DynamoDB, each record in a table is uniquely identified by the primary key for your table. You can use this primary key to ensure there is not an existing record with the same primary key. To do so, you would use a Condition Expression to prevent writing an item if an item with the same key already exists.

Does GSI need to be unique?

However, the key values in a global secondary index do not need to be unique.

Which one of the following data types does Amazon DynamoDB not support?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type. It can be useful instead to store data and time data as a number data type, using Unix epoch time.

What is secondary index in DynamoDB?

A secondary index is a data structure that contains a subset of attributes from a table, along with an alternate key to support Query operations. You can retrieve data from the index using a Query , in much the same way as you use Query with a table.


1 Answers

Short answer: No.

DynamoDB is a key:value store. It is very good at quickly retrieving/saving Items because it does a couple of compromise. This is a constraint you have to handle yourself.

Nonethess, depending on your actual model, it might be a good idea to use this field as you hash_key or consider using a range_key

If this is not possible, I advise you to de-normalize your data. You currently have something like:

UserTable

  • hash_key: user_id
  • e-mail
  • ...

To ensure unicity, add a new table with this schema:

EmailUser

  • hash_key: e-mail
  • user_id

To make sure an e-mail is unique, just issue a GetItem to EmailUser before.

This kind of de-normalization is quite common with No-SQL databases.

like image 107
yadutaf Avatar answered Sep 19 '22 21:09

yadutaf