Using a ProjectionExpression with reserved words with Boto3 in DynamoDB
Question:
I’m selecting data from my DynamoDB database using boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)
response = table.scan(ProjectionExpression='Id,Name')['Items']
Works fine. Now I also want to retrieve an attribute that is (unfortunately) named with a reserved word – let’s say CONNECTION
.
response = table.scan(ProjectionExpression='Id,Name,Connection')['Items']
I get an error like
An error occurred (ValidationException) when calling the Scan
operation: Invalid ProjectionExpression: Attribute name is a reserved
keyword; reserved keyword: Connection
I know there’s an aliasing technique if using filters or queries, but does this exist for simple projections from boto3?
Answers:
Turns out that this is easily solved the same as when calling the DynamoDB API directly.
We should use an alias for any reserved word, and then provide a mapping from the alias back to the ‘true’ name with the ExpressionAttributeName parameter/property.
response = table.scan(ProjectionExpression = 'Id, Name, #c',
ExpressionAttributeNames = {'#c': 'Connection'})['Items']
I’m selecting data from my DynamoDB database using boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)
response = table.scan(ProjectionExpression='Id,Name')['Items']
Works fine. Now I also want to retrieve an attribute that is (unfortunately) named with a reserved word – let’s say CONNECTION
.
response = table.scan(ProjectionExpression='Id,Name,Connection')['Items']
I get an error like
An error occurred (ValidationException) when calling the Scan
operation: Invalid ProjectionExpression: Attribute name is a reserved
keyword; reserved keyword: Connection
I know there’s an aliasing technique if using filters or queries, but does this exist for simple projections from boto3?
Turns out that this is easily solved the same as when calling the DynamoDB API directly.
We should use an alias for any reserved word, and then provide a mapping from the alias back to the ‘true’ name with the ExpressionAttributeName parameter/property.
response = table.scan(ProjectionExpression = 'Id, Name, #c',
ExpressionAttributeNames = {'#c': 'Connection'})['Items']