10
votes

I try to query my dynamoDB from a Lambda function. My table uses "id" as the hash key. I tried both versions below and received the corresponding error messages. What am I doing wrong?

  var params = {
        TableName : "addresses",
        KeyConditionExpression: "id = :id AND city = :city",
        ExpressionAttributeValues: {
            ":id": "Austria",
            ":city": "Salzburg"
        }
    };

Unable to query. Error: { "message": "Query key condition not supported",...}

var params = {
    TableName : "addresses",
    KeyConditionExpression: "city = :city",
    ExpressionAttributeValues: {
        ":city": "Salzburg"
    }
};

Unable to query. Error: { "message": "Query condition missed key schema element: id",...}

EDIT:

I now added secondary indices, but still get the same errors:

enter image description here

1
quering for KeyConditionExpression: "id = :id" works? and do you have range key? - Eyal Ch
Thanks for the hint. I just tried only using the id without the city condition and it works. How can I add additional conditions? I do not use a range key - Chris
i will answer it (to get +1 :) ) - Eyal Ch

1 Answers

14
votes

if your hash key is 'id' then you cant query by:

KeyConditionExpression: "id = :id AND city = :city"

or by:

KeyConditionExpression: "city = :city"

you can query dynamodb only by hash and range key.

so your query should contain always hash key (id). if you want to query by 'city' also, you should add 'city' as range key to dynamodb table (or local secondary index)

then you can query for a record with 'id' and 'city'.

update:

if you want to query for 'city'

KeyConditionExpression: "city = :city"

then you can just add global secondary index to the table.