0
votes

I'm trying to do this query in the local DynamoDB but I don't know why it doesn't work

Error:

Unable to read item. Error JSON: { "message": "Query condition missed key schema element", "code": "ValidationException", "time": "2020-04-13T23:49:24.297Z", "requestId": "99bcabdb-7168-4b26-8056-74482d92ac42", "statusCode": 400, "retryable": false, "retryDelay": 23.68974772992818 }

Query

  var params = {
    TableName: TESTE,
    IndexName: 'indexCNPJ',
    KeyConditionExpression: 'cep = :cep',
    ExpressionAttributeValues: {
      ':cep': cep
    }
  };

  docClient.query(params, function (err, data) {
    if (err) {
      console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
      console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
      res.status(200).json(data)
    }
  }); ```

**Table** 

   ``` var params = {
    TableName : "TESTE",
    KeySchema: [
        { AttributeName: "CEP", KeyType: "HASH"},
        { AttributeName: "CNPJ", KeyType: "RANGE"}
    ],
    AttributeDefinitions: [
        { AttributeName: "CEP", AttributeType: "N" },
        { AttributeName: "CNPJ", AttributeType: "S" }
    ],
    GlobalSecondaryIndexes: [
        {
          IndexName: 'indexCNPJ',
          KeySchema: [
            { AttributeName: 'CEP', KeyType: 'HASH' },
            { AttributeName: 'CNPJ', KeyType: 'RANGE' }
          ],
          Projection: {
            ProjectionType: 'ALL'
          },
          ProvisionedThroughput: {
            ReadCapacityUnits: 10,
            WriteCapacityUnits: 10
          }
        }
      ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

The only thing i wanted was just that when calling the service just pass the zip parameter. Because, it is not possible to create the table only with the zip code as HASH is that in the records there are duplicates

enter image description here

At first I only have the cep parameter. Does anyone know how I can solve this problem?

1

1 Answers

1
votes

Query condition missed key schema element this mean your query does not include hash key.

Your indexCNPJ index schema is:

KeySchema: [
            { AttributeName: 'CEP', KeyType: 'HASH' },
            { AttributeName: 'CNPJ', KeyType: 'RANGE' }
          ],

This means, CEP is hash key. Check again your query,

...
KeyConditionExpression: 'cep = :cep',
...

It does not include hash key - CEP. You use cep instead of CEP, then you get back the error.

Let's correct your attribute name in the KeyConditionExpression property:

...
KeyConditionExpression: 'CEP = :cep',
...

Btw, indexCNPJ index has the same schema with your table index schema, why you need create a GSI index the same with table index? I think it is not necessary. In this case, just query with your table index without GSI.