Reputation: 7213
I am trying to update Dynamo Table using expression, I am trying to generate keys dynamically. The issue looks like if they key has space in it, expressions don't evaluate properly.
Following in my update implementation:
update_expression = 'SET {}'.format(','.join(f'#{p}=:{p}' for p in row_as_dict))
expression_attribute_values = {f':{p}': v for p, v in row_as_dict.items()}
expression_attribute_names = {f'#{p}': p for p in row_as_dict}
if result_set.get('Item') and result_set.get('Item').get(primary_key):
TABLE.update_item(
Key={
primary_key: row_as_dict[primary_key],
sort_key: row_as_dict[sort_key]
},
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ExpressionAttributeNames=expression_attribute_names
)
if i print the values for update_expression and expression_attribute_values and expression_attribute_names i get following output. update_expression
SET #Ref No=:Ref No,#FT/Hot=:FT/Hot,#Irfan watch List=:Irfan watch List,#F.Soahil watchList=:F.Soahil watchList,#HR Responible=:HR Responible,#Pipeline(Adv/Ref)=:Pipeline
(Adv/Ref),#Source=:Source,#Date Submitted=:Date Submitted,#Name=:Name,#Candidate Location=:Candidate Location,#Tech =:Tech ,#Current Company=:Current Company,#Exp=:Exp,#Position being considered for=:Position being considered for,#Proj. / Gen Hiring=:Proj. / Gen Hiring,#Comments / Latest Status / Next Step=:Comments / Latest Status / Next Step,#Status=:Status,#hiring status=:hiring status,#Link to Lever=:Link to Lever,#LinkedIn Link / Resume=:LinkedIn Link / Resume,#Interview Feedback Google Doc=:Interview Feedback Google Doc,#Email=:Email
expression_attribute_values
{':Ref No': '1', ':FT/Hot': 'NaN', ':Irfan watch List': 'NaN', ':F.Soahil watchList': 'NaN', ':HR Responible': 'Khurram', ':Pipeline\r\n(Adv/Ref)': 'Referred', ':Source': 'Usman Khan', ':Date Submitted': '17-Dec', ':Name': 'Asif Mahmood Mughal Zain', ':Candidate Location': 'ISL', ':Tech ': 'Spark. Hadoop\r\nSQL, Big Data', ':Current Company': 'Zain Telecom', ':Exp': '18+', ':Position being considered for': 'Senior SA', ':Proj. / Gen Hiring': 'General', ':Comments / Latest Status / Next Step': 'Muhammaf Naseer Recommneded\r\nAdeel Ashraf Recommended\r\nFaheem Khan Recommended\r\n\r\nAccepted offer.will join on 6 jan', ':Status': 'Joined', ':hiring status': 'Hired', ':Link to Lever': 'Link to Lever', ':LinkedIn Link / Resume': 'Asif Mehmood Mughal', ':Interview Feedback Google Doc': 'Link to Feedback', ':Email': '[email protected]'}
expression_attribute_names
{'#Ref No': 'Ref No', '#FT/Hot': 'FT/Hot', '#Irfan watch List': 'Irfan watch List', '#F.Soahil watchList': 'F.Soahil watchList', '#HR Responible': 'HR Responible', '#Pipeline\r\n(Adv/Ref)': 'Pipeline\r\n(Adv/Ref)', '#Source': 'Source', '#Date Submitted': 'Date Submitted', '#Name': 'Name', '#Candidate Location': 'Candidate Location', '#Tech ': 'Tech ', '#Current Company': 'Current Company', '#Exp': 'Exp', '#Position being considered for': 'Position being considered for', '#Proj. / Gen Hiring': 'Proj. / Gen Hiring', '#Comments / Latest Status / Next Step': 'Comments / Latest Status / Next Step', '#Status': 'Status', '#hiring status': 'hiring status', '#Link to Lever': 'Link to Lever', '#LinkedIn Link / Resume': 'LinkedIn Link / Resume', '#Interview Feedback Google Doc': 'Interview Feedback Google Doc', '#Email': 'Email'}
The Error generated is
An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeValues contains invalid key: Syntax error; key: ":hiring status"
Upvotes: 1
Views: 1221
Reputation: 2092
You might be very interested in dynamof. Its a library purposed to do exactly what your trying to do. At the very least you can take a peak and check out how it handles dynamically creating all the expressions.
I suggest you take a look at the args module.
It has the functions that do what your trying to do. The args module gets some help from another module (the request builder) in dynamof
that is responsible for some preprocessing of the entire action's arguments so the args module's functions get an easy to use and parse RequestTree object.
I would just use dynamof
- but I'm biased. If you want to write it yourself, heres a little example on how its been done straight from dynamof
.
def UpdateExpression(request: RequestTree):
def expression(attr):
if attr.func is not None:
return attr.func.expression(attr)
return f'{attr.alias} = {attr.key}'
key_expressions = [expression(key) for key in request.attributes.values]
key_expression = ', '.join(key_expressions)
return f'SET {key_expression}'
NOTE: The attr.alias
is whats used to handle the special name cases - like spaces. Its set in the request builder module mentioned above.
disclaimer: I wrote dynamof
Upvotes: 0
Reputation: 8887
You can't have spaces in your attribute keys/values in the expression. So :hiring status
needs to be something like :hiring_status
.
Upvotes: 1