Common DynamoDB KeyConditionExpression Errors

Common DynamoDB KeyConditionExpression Errors

by admin April 12, 2020

DynamoDB is a powerful NoSQL database powered by AWS. We highly recommend using DynamoDB as part of our ideal stack for 2020. As with any technology, you’re prone to running into a few common DynamoDB KeyConditionExpression errors.

Most commonly, you’ll be calling DynamoDB from a Lambda function. If you’ve fiddled around with the DynamoDB Python library as a beginner to the platform, it’s likely that you’ve seen this error message:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.


Contrary to what may have been mentioned in past documentation, KeyConditionExpression IS a required parameter when running a DynamoDB Query.

When querying DynamoDB, the KeyConditionExpression helps dynamo determine which partitions to search for your data. Remember, DynamoDB is a NoSQL database solution, so we’re modeling our data slightly different than we would do in the traditional relational database model.

When accessing DynamoDb, we recommend setting up a SINGLE table with a partition key (pk) and a sort key (sk) that allow you to creatively index your data. Unlike traditional relational databases, you will NOT have a separate table for each model in your database. In fact, we usually only use ONE table across an entire application. This helps make DynamoDB more performant and scalable. It’s why you’ll often see a latency DECREASE when companies get a significant increase in load. DynamoDB is anti-fragile.

So how do we execute a query?

query = table.query(

Note, in this query, we have a partition key equal to the invoice and the ID of that invoice. We specify the type of key in the pk, because we’ll have multiple ‘object’ types in our schema, and it helps us organize our data for more effective queries.

What’s the value of the sort key? Let’s say we want to do run a query on all of the items for a particular invoice. Our query would look something like this:

query = table.query(
KeyConditionExpression=Key('pk').eq('invoice|120234') & Key('sk').begins_with('item|')

This will return all fo the items for that invoice. See how simple a has many query can be?

Remember, it’s required to include the KeyConditionExpression when running a DynamoDB query. If you don’t include it, your query won’t execute.  If you run into any other common DynamoDB KeyConditionExpression errors, let us know!

Social Shares

Related Articles