user1187968
user1187968

Reputation: 7986

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?

Upvotes: 5

Views: 10947

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

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'}

Upvotes: 6

Related Questions