Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 query using KeyConditionExpression

I'm having trouble understanding why below query on a DynamoDB table doesn't work:

dict_table.query(KeyConditionExpression='norm = :cihan', ExpressionAttributeValues={':cihan': {'S': 'cihan'}})

and throws this error:

ClientError: An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type

while the following works:

dict_table.query(KeyConditionExpression=Key('norm').eq('cihan'))

norm is a field with type string. I'm using boto3 v 1.4.0 and following the docs:

In [43]: boto3.__version__
Out[43]: '1.4.0'

Can anyone see what's the error in the first query?

Bonus question: What's with all the tokens and the need to replace them all the time? Why can't I just say dict_table.query(KeyConditionExpression='norm = cihan')

like image 410
tayfun Avatar asked Sep 21 '16 21:09

tayfun


People also ask

Can we Query DynamoDB with sort key?

Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results. For more information on how to use Query , such as the request syntax, response parameters, and additional examples, see Query in the Amazon DynamoDB API Reference.

How do you get a table on boto3?

Step 1 − Import boto3 and botocore exceptions to handle exceptions. Step 2 − database_name and table_name is the mandatory parameter. It fetches the definition of given table. Step 3 − Create an AWS session using boto3 library.

How does boto3 connect to DynamoDB?

Connecting AWS resources to the python environment requires a boto3 package. Creating a dynamo DB client is a connection instance that lets us connect with our dynamo DB service. We need to specify region_name , aws_access_key_id , aws_secret_access_key in order to connect with our dynamoDb service.


1 Answers

Please change the ExpressionAttributeValues as mentioned below.

ExpressionAttributeValues={':cihan': 'cihan'}
like image 86
notionquest Avatar answered Sep 20 '22 14:09

notionquest