I have a table with a keySchema of object as HASH and id as RANGE, and a GlobalSecondaryIndex called dateIndex with a keySchema of artificialPK as HASH, date as RANGE
I can query the table using object and id just fine, but when I query the GSI using date and artificialPK, I get a ValidationException of a missing key schema element 'object'. But that's not part of the index keySchema.
How do I query a GSI? All the docs & examples seem to use just the keySchema defined in the GSI, but that's not working for me. Am I missing something obvious?
edit: somehow the GSI has been declared with a different keyschema from the create_table command below, hence the problem.
>>> ddb_resource.create_table(
TableName=self.table_name,
KeySchema=[
{'AttributeName': 'object', 'KeyType': 'HASH'},
{'AttributeName': 'id', 'KeyType': 'RANGE'}
],
AttributeDefinitions=[
{'AttributeName': 'object', 'AttributeType': 'S'},
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'artificialPK', 'AttributeType': 'N'},
{'AttributeName': 'date', 'AttributeType': 'N'}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'dateIndex',
'KeySchema': [
{'AttributeName': 'artificialPK', 'KeyType': 'HASH'},
{'AttributeName': 'date', 'KeyType': 'RANGE'}
],
'Projection': {
'ProjectionType': 'ALL'
},
}
]
)
>>> resp = self.table.query(KeyConditionExpression=Key('object').eq('foo') & Key('id').gt('0'))
>>> len(resp['Items'])
123
>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('date').gt(0) & Key('artificialPK').eq('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: object
>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('object').eq('foo') & Key('id').gt('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: date
>>> resp=self.table.query(IndexName='dateIndex', KeyConditionExpression=Key('date').gt(0) & Key('object').eq('foo') & Key('id').gt('1'))
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Conditions can be of length 1 or 2 only
The first one should work, it looks perfect to me.
Are you sure you're accessing the table and that the index that has the keys that you expect, check the DescribeTable
output.