Reputation: 1055
I am trying to update items in DynamoDB table. The code that I have written is updating the item but when I add a column with the header "source/target", it is giving a "ValidationException" Exception.
Code used to update -
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
response = table.update_item(
Key={
'id': "test_id
},
UpdateExpression="set source/target= :st, user_name= :usr",
ExpressionAttributeValues={
':st' : event['source/target'],
':usr' : event['user_name']
},
ReturnValues="UPDATED_NEW"
)
The error I get is -
An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: \"/\", near: \"source/target\""
How to solve this?
Upvotes: 1
Views: 6697
Reputation: 569
Hi you have to use ExpressionAttributeNames when u have any special character as follows
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
response = table.update_item(
Key={
'id': "test_id"
},
UpdateExpression="set #colName= :st, user_name= :usr",
ExpressionAttributeValues={
':st' : event['source/target'],
':usr' : event['user_name']
},
ExpressionAttributeNames={
'#colName' : 'source/target'
},
ReturnValues="UPDATED_NEW"
)
Upvotes: 6
Reputation: 14799
As you have used a special character in the attribute name, I think you will need to specify a ExpressionAttributeNames
.
I don't know the exact syntax but it should be something like:
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("test")
response = table.update_item(
Key={
'id': "test_id"
},
UpdateExpression="set #src = :st, user_name = :usr",
ExpressionAttributeValues={
':st' : event['source/target'],
':usr' : event['user_name']
},
ExpressionAttributeNames={
'#src' : 'source/target'
},
ReturnValues="UPDATED_NEW"
)
Upvotes: 3