简体   繁体   中英

boto3 DynamoDB - Query operation: ExpressionAttributeNames contains invalid key

I have the following source code to filter items from a DynamoDB.

    ...
    session = boto3.session.Session()
    db = session.resource('dynamodb', region_name=region, endpoint_url=endpoint)
    self.table_obj = db.Table(table_name)

    filter_expression = ':status_name <> :status_val'
    attr_names = {'status_name': 'status'}
    attr_values = {'status_val': 'UPDATED'} 

    response = table.query(FilterExpression=filter_expression,
                           ExpressionAttributeNames=attr_names,
                           ExpressionAttributeValues=attr_values)

However, I getting the following error:

ClientError: An error occurred (ValidationException) when calling the Query operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "status_name"

I can't see anything wrong with the above code, am I missing something?

As per the Expression Attribute Name documentation

An expression attribute name is a placeholder that you use in an Amazon DynamoDB expression as an alternative to an actual attribute name. An expression attribute name must begin with a pound sign (#), and be followed by one or more alphanumeric characters.

I think you are missing the pound(#) in from of your placeholder attribute names.

Try to change this

attr_names = {'status_name': 'status'}

to

attr_names = {'#status_name': 'status'}

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