lorisleitner
lorisleitner

Reputation: 698

Cannot query DynamoDB using QueryAsync and partition key

My data model looks like this:

[DynamoDBTable("prices")]
public class Price
{
    [DynamoDBHashKey("id")]
    public String Id { get; set; }

    [DynamoDBProperty("symbol")]
    public string Symbol { get; set; }

    [DynamoDBProperty("expdate")]
    public long ExpDate { get; set; }
}

I'm trying to query DynamoDB using QueryAsync like this:

var q = db.QueryAsync<Price>("ID");

I'm always getting a InvalidOperationException despite only using the partition key to query my table.

System.InvalidOperationException: 'Must have one range key or a GSI index defined for the table prices'

Why am I getting this error and what can I do to resolve it?

Thanks

Upvotes: 0

Views: 2950

Answers (1)

Hauwk
Hauwk

Reputation: 31

If your table has just a Hash key then you should use the "LoadAsync" method instead of QueryAsync. It will return null if the key does not exist.

var item = await _db.LoadAsync<Price>("ID");

If your table has a simple primary key (partition key), you can't use the Query method. Instead, you can use the Load method and provide the partition key to retrieve the item.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DotNetDynamoDBContext.html#w20aac17c17c21c35c31

Upvotes: 3

Related Questions