1

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

6
  • the table name nor the key name match from the error, query to your table schema, can you fix those and make sure you are posting the right error? Commented Aug 10, 2021 at 15:27
  • That error says to me that the table has a sort key and you are not including it. Commented Aug 10, 2021 at 15:33
  • @Kirk No, the table doesn't have a sort key, I checked it in the AWS console Commented Aug 10, 2021 at 15:46
  • @WarrenParad I fixed the wrong names, the error is the same. I noticed the AWS docs say that you can't query a table without a sort key using the .NET Object Persistence Model. What's the reason behind that? I can query tables using AmazonDynamoDBClient but not DynamoDBContext when a sort key isn't defined?docs.aws.amazon.com/amazondynamodb/latest/developerguide/… Commented Aug 10, 2021 at 15:47
  • because it sucks? Use the abstracted object model and not the Persistence Model, it doesn't moke any sense Commented Aug 10, 2021 at 17:09

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I found that too but it's weird, I can query a table with just a hash key using the Java provider with no problems. It's a design flaw in the .NET provider. I created a GitHub issue for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.