简体   繁体   中英

Using query IndexName, GSI in DynamoDB and not available begins_with in python

I am following the documentation and I see that "begins_with" is available to use in python 3.7, but I am getting the next error if I want to query a table with GSI "createdAt-index".

So here is the code that doesn't works:


def query_pharmaorders(date):
    table = dynamodb.Table('users')
    response = table.query(
        IndexName="createdAt-index",
        KeyConditionExpression=Key('createdAt').begins_with("2020"),
    )
    return (response['Items'])

Here is the error:


Response
{
  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 19)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 19\n    \t\tKeyConditionExpression=Key('createdAt').begins_with(\"2020\")\n"
  ]
}

Where my partition Key is createdAt.

Any ideas?

You don't say what the error is, but have a few ideas of what might be wrong.

According to the docs for the query operation

Note that if you use the IndexName parameter, you must also provide TableName.

When using the query operation, the partition key equality test is required and must be specified in the following format:

partitionKeyName =:partitionkeyval

While you can use a begins_with condition on the sort key, you can't do so with the partition key. You'll need to specify the exact partition key when using the query operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM