I’m using python to query a dynamodb table,however I’m using two keys in the getitem call that are not part of the primary key or sort key.I created a global index that contains these two keys, but I still get the same error.
response = table.get_item(
Key={
'player_id': 22892251,'type': 1
}
)
item = response['Item']
print(item)
2
Answers
You cannot issue a
GetItem
against a secondary index as items are not unique.You must use a
Query
request.You’re trying to use
GetItem
to fetch data from a global secondary index. This is not supported. TheGetItem
API returns exactly 1 item, which is only possible because the Primary Key (Partition + Sort Key) is guaranteed to be unique in the base table.This is not the case for global secondary indexes, which is why
GetItem
is not supported here. It requires a guarantee that the underlying data structure does not give.The way to fetch this data is to use the
Query
operation that can return multiple items:It’s on your app to ensure that the entries are unique if you require it. Here’s an example that lets you detect if this assumption got broken somehow.