3
votes

I'm trying to update an item using a Global Secondary Index. My table definition is listed below. I am new to DynamoDb.

    $response = $this->client->createTable([
        'TableName' => 'rawproducts_products',
        'AttributeDefinitions' => [
            [
                'AttributeName' => 'product_code',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'token',
                'AttributeType' => 'S'
            ],
            [
                'AttributeName' => 'processed_at',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'created_at',
                'AttributeType' => 'N'
            ]                    
        ],
        'KeySchema' => [
            [
                'AttributeName' => 'product_code',
                'KeyType' => 'HASH' 
            ],
            [
                'AttributeName' => 'token',
                'KeyType' => 'RANGE' 
            ]
        ],
        'LocalSecondaryIndexes' => [
            [
                'IndexName' => 'ProductCodeProcessedIndex',
                'KeySchema' => [
                    ['AttributeName' => 'product_code', 'KeyType' => 'HASH'],
                    ['AttributeName' => 'processed_at', 'KeyType' => 'RANGE']
                ],
                'Projection' => [
                    'ProjectionType' => 'KEYS_ONLY',
                ],
            ],
            [
                'IndexName' => 'ProductCodeCreatedIndex',
                'KeySchema' => [
                    ['AttributeName' => 'product_code', 'KeyType' => 'HASH'],
                    ['AttributeName' => 'created_at', 'KeyType' => 'RANGE']
                ],
                'Projection' => [
                    'ProjectionType' => 'KEYS_ONLY',
                ],
            ]                
        ],
        'GlobalSecondaryIndexes' => [
            [
                'IndexName' => 'TokenIndex',
                'KeySchema' => [
                    [ 'AttributeName' => 'token', 'KeyType' => 'HASH' ]
                ],
                'Projection' => [
                    'ProjectionType' => 'ALL'
                ],
                'ProvisionedThroughput' => [
                    'ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1 
                ] 
            ] 
        ],            
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'    => 5,
            'WriteCapacityUnits' => 6
        ]
    ]);

When I try to update the attribute "complete" using the query below:

    $this->dynamoDb->updateItem([
        'TableName' => $this->table,
        'TableIndex' => 'TokenIndex',
        'Key'   =>  [
            'token'    =>  ['S' => (string)$this->token['S']]
        ],
        'UpdateExpression'  =>  'set complete = :complete',
        'ExpressionAttributeValues' =>  [
            ':complete' =>  ['N'   => (string)1]
        ]
    ]);

But I keep getting the following error:

    "The provided key element does not match the schema"

Can anybody please advise a newbie. Many thanks.

1

1 Answers

7
votes

It shows this because you are not passing key properly

You need to pass product_code and token both to update the value

Secondly

You cannot update value directly from GSI it's just a projection of the Table and not actual table

If you want to update the value you have to update in the table not in the index

refer this link.