28

I am looking at using DynamoDb for one of my applications.

I wanted to know if it's possible to set an attribute as range key and then leave it unpopulated for certain records(either empty or null).

somesh
  • 2,509
  • 3
  • 16
  • 24

1 Answers1

38

You can't insert an item with Range Key as empty value or without the Range key.

If the Range key is not included. You would get the below error:-

"One of the required keys was not given a value"

If the Range key is populated with '' (i.e. empty string)

"message": "One or more parameter values were invalid: An AttributeValue may not contain an empty string"

From AWS Doc:-

When you add an item, the primary key attribute(s) are the only required attributes. Attribute values cannot be null. String and Binary type attributes must have lengths greater than zero. Set type attributes cannot be empty. Requests with empty values will be rejected with a ValidationException exception.

notionquest
  • 37,595
  • 6
  • 111
  • 105
  • Thanks @notionquest. I was searching for the issue from many hours. – sohal Jun 20 '18 at 05:33
  • It is a very old post. Just adding the updated answer. It accepts empty for non-key string attributes https://aws.amazon.com/about-aws/whats-new/2020/05/amazon-dynamodb-now-supports-empty-values-for-non-key-string-and-binary-attributes-in-dynamodb-tables/ – mark Jan 06 '21 at 21:56
  • 1
    Partition key for GSI can be omitted from the record, I think LSI works the same way see: https://stackoverflow.com/questions/48575985/how-can-i-make-a-sparse-index-if-the-key-is-always-required/48633818 – Abul Fayes May 18 '21 at 17:13