Reputation: 4843
I am trying to get a gt()
query working with a DynamoDB GSI
My Cloudformation is as follows -
Resources
MyTable:
Properties:
AttributeDefinitions:
- AttributeName: pk
AttributeType: S
- AttributeName: sk
AttributeType: S
- AttributeName: my_datetime_string
AttributeType: S
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: my_datetime_string-index
KeySchema:
- AttributeName: my_datetime_string
KeyType: HASH
Projection:
ProjectionType: ALL
KeySchema:
- AttributeName: pk
KeyType: HASH
- AttributeName: sk
KeyType: RANGE
TableName:
Fn::Sub: my-table-${AWS::StackName}-${AWS::Region}
Type: AWS::DynamoDB::Table
My query code is as follows (python) -
tablename="whatever"
import boto3
table=boto3.resource("dynamodb").Table(tablename)
from datetime import datetime
timestamp="%s 00:00:00" % datetime.today().strftime("%Y-%m-%d")
from boto3.dynamodb.conditions import Key
expr=Key("my_datetime_string").gt(timestamp)
print (table.query(IndexName="my_datetime_string-index",
KeyConditionExpression=expr))
I have some data in the table and have can see that it has a String type (my_datetime_string
) -
{'Item': {'my_datetime_string': '2022-01-27 14:30:00', 'sk': 'HEAD', 'pk': 'EVENT#1293000'}, 'ResponseMetadata': {'RequestId': 'XXX', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 08 Feb 2022 10:54:15 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '522', 'connection': 'keep-alive', 'x-amzn-requestid': 'XXX', 'x-amz-crc32': '2080419831'}, 'RetryAttempts': 0}}
But all I get from the query is -
An error occurred (ValidationException) when calling the Query operation: Query key condition not supported
What am I missing here ? Surely GSI indexes support gt()
queries for String
types ? (or maybe not ??)
Upvotes: 0
Views: 681
Reputation: 25649
my_datetime_string
is your index's Partition Key
(= Hash Key
). A Query must provide one-and-only-one Partition Key
value. This means that eq
is a valid operator in Partition Key
conditions, but gt
is not.
If your table/index has a compound primary key, a query can apply a gt
operation to the Sort Key
(= Range Key
) condition, which will return a range of values.
Upvotes: 2