1
votes

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

Not sure if that completely answers my use case.

I have 3 attributes in dynamodb.

| A | B | C |

All A and B both attributes are unique across the entire table. I mean there can be 2 items

A = 1, B = 2 ; A = 2, B = 1

but there cannot be 2 items

A = 1, B = 1; A = 2, B = 1
OR
A = 1, B = 1; A = 1, B = 2

How can I insert a new value in that dynamodb (without denormalizing that table) such that I don't break that DB constraints? I think conditional expressions work off primary key http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html and Transaction manager https://java.awsblog.com/post/Tx13H2W58QMAOA7/Performing-Conditional-Writes-Using-the-Amazon-DynamoDB-Transaction-Library also might not work since I think it detects changes to the records retrieved with same primary key attribute and not with the other unique attribute

1

1 Answers

0
votes

I am afraid that this is not possible in dynamoDB, since it is a key-value store.

Take a look on this SO question: Is there a way to enforce unique constraint on a property (field) other than the primary key in dynamodb

I am not sure how are you working with dynamoDB but if you are managing your data with Java aws-sdk, you might be lucky, since the Mapper class offers a way how to specify expressions when performing some operations. You could put the conditions for uniqueness into the expressions to enforce the execution only if all the conditions are met.

Have a look on THIS article which describes how to use expressions with dynamoDB. This code was taken from that article:

try {
   DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
   Map expected = new HashMap();
   expected.put("status", 
      new ExpectedAttributeValue(new AttributeValue("READY").withExists(true));

   saveExpression.setExpected(expected);

   mapper.save(obj, saveExpression);
} catch (ConditionalCheckFailedException e) {
   // This means our save wasn't recorded, since our constraint wasn't met
   // If this happens, the worker can simply look for a new task to work on
}