2

I have dynamodb table with GSI as the following:

ParentId#ChildName
2346#John
2388#Jerry

Now I want to add a new time to the table where parentId = 2388 and ChildName = Tom, before adding this item I want to make sure that the GSI doesn't already contains this combination "2388#Tom".

How can I achieve this, I was thinking of using a condition expression on GSI while doing the putItem operation, but I am not sure if dynamo db support condition expression on secondary indexes?

Any help will be highly appreciated.

Arun Kumar
  • 23
  • 5

1 Answers1

1

You cannot do it with the table structure you have. GSIs are eventually consistent and can’t be used in a write, even with transactions. You can succeed if you manage the GSI propagation yourself to another item or, as this answer mentions, another table.

DynamoDBSaveExpression with conditional check on GSI

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
  • Thanks @hunterhacker for your answer. This approach to propagate data to another table, is it not an anti-pattern? – Arun Kumar Mar 27 '22 at 18:12
  • It’s not something you do unless it’s the only way. Can you instead change your base table to have that as the PK? – hunterhacker Mar 28 '22 at 03:06
  • My base table is already using another attribute as PK which is also necessary as I need to support as lot of update query based on that attribute. But I thought of another idea, I think can I use index-overloading on my Primary key here and instead of propagating this data to a new table, I can just add it as a PK in the same table. So now my PK will be storing multiple type of attributes and I just need to be careful in framing my queries. – Arun Kumar Mar 28 '22 at 07:35
  • Sure. In my answer I said another item or another table. By saying “another item” I meant within the same table. – hunterhacker Mar 28 '22 at 16:03