0

I'm having problems creating a simple query request. Here they show an example of how to do a query using the global secondary index. Now in this case, I just have the primary key, and I'd like to query from my table. This is currently the error that I'm getting:

Query condition missed key schema element: id

Here is what I'm currently trying:

var params = {
  TableName : "XactRemodel-7743-DynamoDBImagesTable-8183NQ0UG0Y5",
  KeyConditionExpression: 'HashKey = :hkey',
  ExpressionAttributeValues: {
    ':hkey': event.projectId
  }
};
documentClient.query(params, function(err, data) {
   if (err) {
     console.log(err)
   } else {
     console.log(data);
   }
});

I know that in the example they used "indexName" which corresponds to the secondary indexes name. They Key Schema doesn't seem to have such an attribute.

This is what my table looks like defined in a YAML file:

DynamoDBImagesTable:
Type: AWS::DynamoDB::Table
Properties:
  BillingMode: PAY_PER_REQUEST
  SSESpecification:
    SSEEnabled: true
  PointInTimeRecoverySpecification:
    PointInTimeRecoveryEnabled: true
  AttributeDefinitions:
    - AttributeName: id
      AttributeType: S
    - AttributeName: companyId
      AttributeType: S
    - AttributeName: lastModified
      AttributeType: S
  KeySchema:
    - AttributeName: id
      KeyType: HASH
  GlobalSecondaryIndexes:
    - IndexName: companyId-index
      KeySchema:
        - AttributeName: companyId
          KeyType: HASH
        - AttributeName: lastModified
          KeyType: RANGE
      Projection:
        ProjectionType: ALL

What am I missing?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Felipe Centeno
  • 2,911
  • 1
  • 21
  • 39

1 Answers1

1

This is trying to query by primary key named HashKey:

KeyConditionExpression: 'HashKey = :hkey',

However your primary key is named id which is what the error message is pointing out. So change that line to:

KeyConditionExpression: 'id = :hkey',
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • You know, looking at it now seems so obvious... ‍♂️, for some reason I thought that was referring to the type of key. Well, thanks so much! – Felipe Centeno Jun 11 '19 at 20:13
  • 1
    Would I be wrong to suggest that this is a case where you should really use [`get()`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property) and not `query()`? Or does it matter? – Michael - sqlbot Jun 11 '19 at 22:18
  • @Michael-sqlbot looking back at this, I believe you're right. Get() should do the work, and it's a little more simple – Felipe Centeno Jul 11 '19 at 16:48