0
votes

I have a table in dynamo DB with the fields A, B, C, D and E. The primary key is A(partition key) and B(sort key).

I want to have another unique constraint for C and D together as a composite unique key. In mysql I would do something like this

ALTER TABLE YourTable
 add CONSTRAINT YourTable_unique UNIQUE (C, D);

I want to do something similar in dynamo DB so that when i create a new entry with an already matching composite unique key(C and D), it does not allow me to create that entry.

2
I doubt this feature is available in DynamoDB, but you can do this at application level - Harshal Bulsara

2 Answers

2
votes

from documentation:

To write an item only if it doesn't already exist,
use PutItem with a conditional expression that uses the 
attribute_not_exists function and the name of the table's 
partition key

you cant add constraint on other keys then partition keys (you cant add constraint on global secondary key)

-1
votes

@Eyal Ch is right. I handled it at the application level, by doing a scan before saving like this :

DynamoDBScanExpression expr = new DynamoDBScanExpression();
expr.addFilterCondition("C",new Condition()
                        .withComparisonOperator(ComparisonOperator.EQ)
                        .withAttributeValueList(new AttributeValue().withS("value of C")));
expr.addFilterCondition("D",new Condition()
                        .withComparisonOperator(ComparisonOperator.EQ)
                        .withAttributeValueList(new AttributeValue().withS("value of D")));
 List<TableClass> responseList = mapper.scan(TableClass.class, expr);
 if (responseList.size() == 0){
 ........//save the record here
 }