prime
prime

Reputation: 2074

DynamoDb - Integer can not be converted to an String

I'm new to dynamoDB and am struggling to populate my DynamoDB table which I have defined the table as follows:

    $response = $DynamoDb->createTable([
        'TableName' => 'products',
        'AttributeDefinitions' => [
            [
                'AttributeName' => 'product_code',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'created_at',
                'AttributeType' => 'N'
            ],
        ],
        'KeySchema' => [
            [
                'AttributeName' => 'product_code',
                'KeyType' => 'HASH' 
            ],
            [
                'AttributeName' => 'created_at',
                'KeyType' => 'RANGE' 
            ]
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'    => 5,
            'WriteCapacityUnits' => 6
        ]
    ]);

and populated using

    $result = $DynamoDb->putItem([
      'TableName' => 'products',
        'Item' => [
          'product_code' => ['N'  =>  (int)$row[0]],
          'created_at' => ['N' => (int)strtotime("now")]
        ]
    ]);

I keep getting the error

Integer can not be converted to an String

Can anybody please advise a newbie. Many thanks.

Upvotes: 5

Views: 8725

Answers (2)

Vijay Chauhan
Vijay Chauhan

Reputation: 61

$result = $DynamoDb->putItem([
      'TableName' => 'products',
        'Item' => [
          'product_code' => ['N'  =>  (string)$row[0]],
          'created_at' => ['N' => (string)time()]
        ]
    ]);

Upvotes: 4

Eyal Ch
Eyal Ch

Reputation: 10056

you should insert to dynamo str values. dynamo will convert it to integer if 'N' is the type of the row

"N": "string", 

 $result = $DynamoDb->putItem([
          'TableName' => 'products',
            'Item' => [
              'product_code' => ['N'  =>  (str)$row[0]],
              'created_at' => ['N' => (str)strtotime("now")]
            ]
        ]);

from documantaion:

Item

Each element in the Item map is an AttributeValue object.

**Type: String to AttributeValue object map**

Upvotes: 14

Related Questions