2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.