lmika
lmika

Reputation: 1755

DynamoDB: Conditional check on Global Secondary Index in Write Transaction

I have a DynamoDB table which is tracking two IDs:

This table has a Global Secondary Index, with the partition key set to B and the sort key set to A; acting as a reverse lookup for the records that are in the main table.

I would like to put a record into this main table, but only do so if there's no record with B already present. Specifically, I'd like to:

  1. Check that B doesn't exist in the GSI
  2. Call out to another system to do something
  3. Put a record with B and a randomly generated A

Is this possible using DynamoDB transactional writes? From looking at the documents, there is a ConditionCheck operation that could be useful here; but as far as I can tell, it doesn't seem possible to run a condition check on a GSI, or am I missing something here?

Upvotes: 3

Views: 1656

Answers (1)

hunterhacker
hunterhacker

Reputation: 7132

You cannot do transactions that include GSIs. I would recommend you follow the design of this blog post, putting an entry in the base table with B as the primary key to enable the uniqueness check (maybe then using it instead of a GSI):

https://aws.amazon.com/blogs/database/simulating-amazon-dynamodb-unique-constraints-using-transactions/

Upvotes: 2

Related Questions