I am trying to have put_item to check if there is item with the same HashKey before actually adding the new item.
According to boto DynamoDB2 document, it is possible to do it with "Conditional Put".
I tried following command but no luck.
connection.put_item('table',item={'locationId':'a1', 'timestamp': time.time()}, expected={'locationID':False})
The error message is as following.
boto.exception.JSONResponseError: JSONResponseError: 400 Bad Request
{u'Message': u'Expected null', u'__type': u'com.amazon.coral.service#SerializationException'}
Does anyone have a conditional put with DynamoDBv2?
Thanks to all in advance.
You need to write it like this for the syntax to work. It's extremely bad that this isn't documented anywhere. I just had the exact same problem and had to try 50 different things while digging through the Java SDK documentation for it to work. Note that you need to give a value if you want to go with 'Exists': True
instead of False
.
connection.put_item(
'table',
item={
'locationId':{'S': 'a1'},
'timestamp': {'N': str(time.time())
},
expected={
'locationID': {'Exists': False}
}
#expected={
# 'locationID': {'Exists': True, 'Value': {'N': '0'}}
#}
)
Hope it helps!
Edit: the question that helped me with the syntax enough to make it work and code in the boto integration tests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With