Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid KeySchema: The second KeySchemaElement is not a RANGE key type

In my Cloudformation script, I'm creating a Dynamo DB table (Datasets) with two keys - let's call them CatalogId and DatasetId. They are both URIs that are outside of my control, but suffice it to say that together they make a unique ID.

I made both of them HASH keys in the primary KeySchema / index. When I did that, CF gave me the following error:

Invalid KeySchema: The second KeySchemaElement is not a RANGE key type

What am I doing wrong?

like image 452
Ryan Shillington Avatar asked Dec 18 '22 07:12

Ryan Shillington


2 Answers

The answer is that only one of the keys can be a HASH key in the primary index. The second key must be of RANGE type, even if you never plan on comparing it with > or <. I'd love it if somebody could elaborate on why I can't have two HASH keys. Why doesn't Dynamo just concatenate the two keys internally and create one primary key?

like image 94
Ryan Shillington Avatar answered Dec 20 '22 21:12

Ryan Shillington


As you mentioned, DynamoDB doesn't have that option. It expects the client to concatenate as String and store that value in one field (i.e. Hash key in the above case).

In case if you still need those attributes as separate fields, you can store that as a non-key attributes individually.

Q: Are composite attribute indexes possible?

No. But you can concatenate attributes into a string and use this as a key.

Example:-

  1. First and last name as composite key
  2. Concatenate first and last name and store that as hash key
  3. Save the first name as a non-key attribute
  4. Save the last name as a non-key attribute

I know it is little redundant. This is just an workaround to keep things clear.

like image 45
notionquest Avatar answered Dec 20 '22 22:12

notionquest