I am trying to update a record in DynamoDB using updateItem
as part of version 3 of the PHP AWS SDK.
The field I am trying to update should contain a numeric value.
If I run this code I get an error:
$params = array(
'TableName' => $table,
'Key' => array('id' => array('S' => $id) ),
'UpdateExpression' => 'set newField = :val',
'ExpressionAttributeValues' => array(
':val' => array('N' => 99)
),
'ReturnValues' => 'ALL_NEW'
);
print_r($params);
$result = $client->updateItem($params);
Error:
Client error: `POST https://dynamodb.eu-west-2.amazonaws.com` resulted in a `400 Bad Request`
response: {"__type":"com.amazon.coral.service#SerializationException","Message":"NUMBER_VALUE cannot be converted to String"}
But if I amend:
':val' => array('N' => 99)
To be:
':val' => array('N' => "99")
Or
':val' => array('N' => (string)99)
Then the request is successful and Dynamo is updated. If I go to the Dynamo console then I can see the updated record and the field correctly shows as a Number
type.
Is there a better way to handle the process of updating DynamoDB records with integers, casting as a string so that the PHP SDK works doesn't seem like the best approach?
All DynamoDB SDKs afaik originally expected numeric values to be presented as strings, but subsequently various marshaling enhancements have brought native type support.
I don't use PHP, but see DynamoDB JSON and Array Marshaling for PHP.