Search code examples
amazon-dynamodbamazon-dynamodb-indexamazon-dynamodb-data-modeling

How to create dynamo db item without specifying gsi key


I am working with a dynamo db table that uses a gsi so I can query via an additional attribute, if it is present. This attribute will be updated in a process so all of the items do not have this attribute in the beginning of the process. This attribute is set as the primary key of the gsi, when added to the item.

Everything works as intended besides the fact that I cannot insert a new item without also specifying the primary key of the gsi. I want the gsi to ignore the item if its primary key is not set yet. I thought this would be possible with the option INCLUDE, when specifying the projected attributes. Obviously I am getting it wrong here but I also don't know how to solve this problem.

Any help is much appreciated and also I will have to stick to dynamo db so every hint including this db helps a lot!

EDIT: For clarification - these are my attributes:

  • id (Primary Key: mandatory)
  • name (Attribute)
  • year (Attribute)
  • gsi_id (GSI Primary Key: optional)

I want to add an item with the fields:

  • id (Primary Key: mandatory)
  • name (Attribute)
  • year (Attribute)

and later add the gsi_id field.


Solution

  • The premise of your question is that

    I cannot insert a new item without also specifying the primary key of the GSI.

    However, I believe this premise is incorrect. According to both my experience and the documentation, you are very much allowed to insert an item without the GSI key attribute to the base table, and this item will be added to the base table but be missing in the index - exactly what you want to happen.

    Here is for example a snippet from the documentation:

    A global secondary index only tracks data items where its key attributes actually exist. For example, suppose that you added another new item to the GameScores table, but only provided the required primary key attributes. ... Because you didn't specify the TopScore attribute, DynamoDB would not propagate this item to GameTitleIndex.

    Note that this is only true for a missing attribute. Something you really are not allowed to do is to set a value with the wrong type in the GSI key attribute. For example, if the GSI key attribute is defined to have "number" type, you cannot set a string in that attribute - you'll get a ValidationException error on the update operation. But having the attribute completely missing is fine.