3
votes

My primary key is a field called "id"

I've added a secondary index to my table on the field "group_number"

enter image description here

I query via the secondary index like so:

// Query the secondary index
queryInput := &dynamodb.QueryInput{
    TableName: aws.String(c.GetDynamoDBTableName()),
    KeyConditions: map[string]*dynamodb.Condition{
        "group_number": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String(c.GroupNumber),
                },
            },
        },
    },
}

However; I get the error "validationexception: query condition missed key schema element: id"

Does DynamoDB only allow the querying of primary keys? I was under the impression you use "GetItem" for primary key being that only one record can come back if you use a primary key. To search via secondary indexes you use "Query", and to search by non-index keys, you use "Scan".

Please let me know what I'm doing incorrectly here.

2

2 Answers

5
votes

In addition to the TableName, You need to also specify the IndexName property when creating the QueryInput to query the index.

https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#QueryInput

// The name of an index to query. This index can be any local secondary index
// or global secondary index on the table. Note that if you use the IndexName
// parameter, you must also provide TableName.
IndexName *string `min:"3" type:"string"`
1
votes
var queryInput, err2 = svc.Query(&dynamodb.QueryInput{
        TableName: aws.String(tableName),
        IndexName: aws.String(index_name),
        KeyConditions: map[string]*dynamodb.Condition{
            "Phone": {
                ComparisonOperator: aws.String("EQ"),
                AttributeValueList: []*dynamodb.AttributeValue{
                    {
                        S: aws.String(phone_no),
                    },
                },
            },
        },
    })