I am trying to query records from DynamoDB table using "IN" comparison operator, below is my code:
import boto3
# Create a DynamoDB client
dynamodb = boto3.client('dynamodb')
# Define the query parameters
table_name = 'tb'
# Perform the query
response = dynamodb.query(
TableName=table_name,
Select='SPECIFIC_ATTRIBUTES',
AttributesToGet=[
'col1',
'col2',
'col3',
'col4'
],
KeyConditions={
'col1': {
'AttributeValueList': [
{
'S':'abc'
},
{
'S':'def'
}
],
'ComparisonOperator': 'IN'
}
}
)
print(response)
I am trying to query all records where ‘col1’ is ‘abc’ or ‘def’, but I got the error:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Attempted conditional constraint is not an indexable operation
2
Answers
For query, only the following operations are supported as KeyConditions:
EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN
Reference: Attempted conditional constraint is not an indexable operation
For your use case, you might want to use multiple queries, or use "scan" (more expensive) instead of "query". Check this post for the difference.
Unfortunately there is no such API as BatchQuery and the
IN
operator is not supported for aKeyConditionExpression
.However, if you would like a workaround you can use PartiQL
ExecuteStatement
:SELECT col1, col2, col3, col4 FROM tb WHERE col1 IN ['ABC', 'DEF']
This will allow you to execute a Query on up to 50 keys.