无效KeyConditionExpression在boto3 dynamodb查询
问题描述:
这是我的表看起来像:无效KeyConditionExpression在boto3 dynamodb查询
toHash
是我的主分区键和timestamp
是排序键。
所以,当我执行此代码[为了得到反向排序timestamp
列表]:从boto3.dynamodb.conditions
进口boto3 导入密钥,的Attr
client = boto3.client('dynamodb')
response = client.query(
TableName='logs',
Limit=1,
ScanIndexForward=False,
KeyConditionExpression="toHash = :X",
)
我得到的以下错误:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :X
我在这里做错了什么? 为什么X
被认为是一个有效的属性值?在查询X`` - :
答
添加ExpressionAttributeValues
下面提到
response = client.query(
TableName='logs',
Limit=1,
ScanIndexForward=False,
KeyConditionExpression="toHash = :X",
ExpressionAttributeValues={":X" : {"S" : "somevalue"}}
)
您需要提供一个''ExpressionAttributeValues''子句查询为''提供值。 – garnaat