dynamoose: [BUG] ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request

Summary:

I am getting the below error. Even though, GSI is configured in the schema.

Code sample:

Schema

const serviceDefinitionSchema = new dynamoose.Schema(
    {
        serviceId: {
            type: String,
            hashKey: true,
            index: true,
        }, // primary key/Hash key
        context: String,
        contextId: {
            type: String,
            index: [
                {
                    global: true,
                    rangeKey: 'serviceId',
                    name: 'contextId-serviceId-index',
                    project: true, // ProjectionType: ALL
                },
                {
                    global: true,
                    rangeKey: 'serviceName',
                    name: 'contextId-serviceName-index',
                    project: true, // ProjectionType: ALL
                },
            ],
        },
        hotelId: {
            type: String,
            index: {
                global: true,
                rangeKey: 'serviceName',
                name: 'hotelId-serviceName-index',
                project: true, // ProjectionType: ALL
                // throughput: 1, // read and write are both 1
            },
        },
        serviceName: String,
        removal: Boolean, // If true, then treat it as Deleted
        isActive: Boolean, // Default: 1
    },
    {
        timestamps: {
            createdAt: 'createDateTime',
            updatedAt: 'lastModifyDateTime',
        },
    }
);

Model

export const ModelOptions = {
    create: true,
    throughput: {
        read: 1,
        write: 1,
    },
    waitForActive: {
        enabled: true,
        check: {
            timeout: 180000,
            frequency: 1000,
        },
    },
    update: false,
};

export const ServiceDefinitionEntity = dynamoose.model(
    process.env.SERVICES_DEFINITION_TABLE ?? 'Appsync-ServicesDefinition',
    serviceDefinitionSchema,
    ModelOptions
);

General

// query
const data = await ServiceDefinitionEntity.query({
            contextId: { eq: sContextId },
        })
            .using('contextId-serviceName-index')
            .where('serviceName')
            .eq(sServiceName)
            // @ts-expect-error
            .exec()
            .catch((error: any) => {
                tools.logError(`s-d.repo > getItemByContextIdAndServiceName > error:`, error);
                throw error;
            });

        tools.logDebug(`s-d.repo > getItemByContextIdAndServiceName > data:`, data);
        return data;

Current output and behavior (including stack trace):

Using above Schema, Model and Query. It throws below error.

s-d.repo > getItemByContextIdAndServiceName > error: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
    at Request.extractError (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\protocol\json.js:51:27)
    at Request.callListeners (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\request.js:688:14)
    at Request.transition (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\request.js:690:12)
    at Request.callListeners (D:\misc\htdocs\spider\hotelservices_service\node_modules\aws-sdk\lib\sequential_executor.js:116:18) {
  code: 'ValidationException',
  time: 2020-08-12T18:07:55.735Z,
  requestId: '9N1VFN8MDRJ8P0659VJVJGPCBJVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 25.984542144410383
}

Expected output and behavior:

It should work as it is working while using hotelId-serviceName-index GSI in the query.

[AWS dynamodb 200 0.462s 0 retries] query({
  ExpressionAttributeNames: { '#qha': 'hotelId', '#qra': 'serviceName' },
  ExpressionAttributeValues: { ':qhv': { S: 'H123456' }, ':qrv': { S: 'Breakfast' } },
  TableName: 'Appsync-ServicesDefinition',
  IndexName: 'hotelId-serviceName-index',
  KeyConditionExpression: '#qha = :qhv AND #qra = :qrv'
})

Environment:

Operating System: Windows Operating System Version: 10 Node.js version (node -v): 12.16.3 NPM version: (npm -v): 6.14.4 Dynamoose version: 2.3.0

Other information (if applicable):

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
  • I have tested the code provided and am confident it doesn’t work as intended
  • I have filled out all fields above
  • I am running the latest version of Dynamoose

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 21 (11 by maintainers)

Most upvoted comments

Please keep this discussion focused and on topic. If you don’t have anything to add to this topic, please refrain from commenting. To show your interest in this issue please consider liking the original issue with a 👍 as opposed to commenting.

If there were updates, I would have posted them here. This is an open source project. So anyone is welcome to contribute and submit a pull request for this issue.

In the event this thread continues to receive off topic comments, I will be locking this thread.

@fishcharlie @lmanerich In 2.7.3, now the error changed to:

ValidationException: Filter Expression can only contain non-primary key attributes: Primary key attribute: columnA

The library is probably constructing the operation parameters wrong. Query’s documentation reads:

A filter expression cannot contain partition key or sort key attributes.

@fishcharlie agree to close as this is no longer reproducible

@lmanerich Ok. I can guarantee you, I won’t be creating a patch off of 2.3.0 for this. So you really do need to upgrade to 2.7.0. As mentioned, I think this is fixed in the latest versions. If you are having issues with 2.7.0, please create new issues for those problems you are having.

I will be closing this issue since it seems to be fixed in 2.7.0 when I tested it.

@lmanerich Actually. Try running the latest version of Dynamoose. I get this error when running it on Dynamoose v2.3.0, but not when running on the latest version of Dynamoose.

Hi @fishcharlie maybe this will help you out:

const dynamoose = require('dynamoose')
dynamoose.aws.ddb.local();

const Tschema = new dynamoose.Schema({
	Id: {
		type: String,
		required: true,
		hashKey: true
	},
    columnA: {
        type: String,
        required: true,
    },
    columnB: {
        type: String,
        required: true,
    },
	resource:{
		type: String,
		index: [{
			global: true,
			name: "resource-index",
            range: "columnA",
			project: true
		},
        {
            global: true,
            name: "resource2-index",
            range: "columnB",
            project: true
        }
        ]
	}
});

IP = dynamoose.model('tests',Tschema)

IP.query("resource").eq("e954609d-ea9c-4f41-96c7-30868fabb10c").using("resource-index").exec((err,result)=>{
	if(err){
		console.log(err)
	}
	console.log(result)
});

I’ve created a local table with 2 GSI:

Hash: resource Range: columnA

Hash: resource Range: columnB

{
  "AttributeDefinitions": [
    {
      "AttributeName": "Id",
      "AttributeType": "S"
    },
    {
      "AttributeName": "resource",
      "AttributeType": "S"
    },
    {
      "AttributeName": "columnA",
      "AttributeType": "S"
    },
    {
      "AttributeName": "columnB",
      "AttributeType": "S"
    }
  ],
  "TableName": "tests",
  "KeySchema": [
    {
      "AttributeName": "Id",
      "KeyType": "HASH"
    }
  ],
  "TableStatus": "ACTIVE",
  "CreationDateTime": "2021-03-04T13:09:48.828Z",
  "ProvisionedThroughput": {
    "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
    "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
    "NumberOfDecreasesToday": 0,
    "ReadCapacityUnits": 3,
    "WriteCapacityUnits": 3
  },
  "TableSizeBytes": 0,
  "ItemCount": 0,
  "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/tests",
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "resource2-index",
      "KeySchema": [
        {
          "AttributeName": "resource",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "columnB",
          "KeyType": "RANGE"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 3,
        "WriteCapacityUnits": 3
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/tests/index/resource2-index"
    },
    {
      "IndexName": "resource-index",
      "KeySchema": [
        {
          "AttributeName": "resource",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "columnA",
          "KeyType": "RANGE"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 3,
        "WriteCapacityUnits": 3
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/tests/index/resource-index"
    }
  ]
}

Resulting in error:

ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
{
  message: 'Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.',
  code: 'ValidationException',
  time: 2021-03-04T13:09:54.990Z,
  requestId: 'ce6c09ed-9878-43a3-9859-d6194673920e',
  statusCode: 400,
  retryable: false,
  retryDelay: 49.91232178527321
}

Any idea of what is causing the problem? Also, a tag “help wanted” could help to find some one in the community with time and knowledge to help.