Here are my use cases: I have a Dynamo table with a hash + range key. When I put new items in the table, I want to do a uniqueness check. Sometimes I want to guarantee that the hash is unique (ignoring the range). Other times I want to allow duplicate hashes, but guarantee that the hash and range combination is unique. How can I accomplish this?
I experimented with attribute_not_exists. It seems to handle the second case, where it checks the hash + key combination. Here's a PHP sample:
$client->putItem(array(
'TableName' => 'test',
'Item' => array(
'hash' => array('S' => 'abcdefg'),
'range' => array('S' => 'some other value'),
'whatever' => array('N' => 233)
),
'ConditionExpression' => 'attribute_not_exists(hash)'
));
Oddly, it doesn't seem to matter if I use attribute_not_exists(hash)
or attribute_not_exists(range)
. They both seem to do exactly the same thing. Is this how it's supposed to work?
Any idea how to handle the case where I only want to check hash
for uniqueness?
attribute_not_exists(any_attribute_that_exists_in_the_table)
returns true if the hash + range combination doesn't exist in the table, otherwise it returns false.attribute_not_exists(attribute_that_does_not_exist_in_the_table)
always returns true. Secondary indexes (global or local) don't affect the behavior.