无效KeyConditionExpression在boto3 dynamodb查询

问题描述:

这是我的表看起来像:无效KeyConditionExpression在boto3 dynamodb查询

enter image description here

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`` - :

+0

您需要提供一个''ExpressionAttributeValues''子句查询为''提供值。 – garnaat

添加ExpressionAttributeValues下面提到

response = client.query(
    TableName='logs', 
    Limit=1, 
    ScanIndexForward=False, 
    KeyConditionExpression="toHash = :X", 
    ExpressionAttributeValues={":X" : {"S" : "somevalue"}} 
)