2016-01-21 40 views
2

我試圖使用全局二級索引來更新項目。我的表格定義如下。我是DynamoDb的新手。DynamoDB - 更新項目由GSI(PHP)

$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 
     ] 
    ]); 

當我嘗試更新屬性「完全」使用下面的查詢:

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

但我不斷收到以下錯誤:

"The provided key element does not match the schema" 

可有人請告知新手。非常感謝。

回答

1

它顯示了這一點,因爲你不及格的關鍵正確

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

其次

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

如果你想更新你在表中的索引

更新不是值

refer this link.