I'm using dynamoDB with the C# driver and I have a table with Users. The table has the following two primary keys:
Then I try to load a User using the Load method on the context like this:
_dynamoDBClient.Context.Load<User>(12345);
I then end up with the following exception:
"exceptionMessage": "Unable to convert range key value for property Created", "exceptionType": "System.InvalidOperationException"
If I load with the specific range key like:
_dynamoDBClient.Context.Load<User>(12345, "2015-01-01");
Everything is ok.
Isn't there a way to load a typed user only using the primary hash key, even though the table has a range key? I don't want to send in the creation date every time I need to get a user. Or have I missunderstood the concept of range keys in dynamoDB?
The Load
method is used to retrieve one single item from your table, therefore you need to provide the entire primary key.
Behind the scenes the Load
method actually calls the GetItem
operation from the native AWS DynamoDB API, which has the following requirements in regards to the attributes that need to be provided:
For the primary key, you must provide all of the attributes. For example, with a hash type primary key, you only need to provide the hash attribute. For a hash-and-range type primary key, you must provide both the hash attribute and the range attribute.
Source: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html
If your table's primary key is composed by Hash and Range Key then you have to provide both to match a single item.
Now when it comes to your data model, the Range Key is used to group related records that are usually retrieved together. In your case I am assuming there are other users with the same UserId
however with a different creation date. If you don't need to group the users together (and sort by creation date) then only having the Hash Key might suffice.
These other two posts might help you to decide the proper key types to use for different scenarios:
What is the use of a Hash range in a dynamodb table? What is the use of a Hash range in a dynamodb table?
When to use what PK type? DynamoDB: When to use what PK type?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With