2017-05-15 7 views
1

Ich übe nur Beispielcode in AWS DynamoDB mit Aber Code-Update funktioniert nicht mit Fehlerpython3 DynamoDB Update_item funktioniert nicht

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: The document path provided in the update expression is invalid for update

Mein Python-Code ist hier. Ich könnte DB ohne 'Karte' Attribut aktualisieren.


table = dynamodb.Table('Movies') 

title = "The Big New Movie" 
year = 2015 

response = table.update_item(
    Key={ 
     "year": year, 
     "title": title 
    }, 
    UpdateExpression="set #attrName.rating = :r, #attrName.plot=:p", 
    ExpressionAttributeNames = { 
     "#attrName" : "info" 
    }, 
    ExpressionAttributeValues={ 
     ':r': decimal.Decimal(5.5), 
     ':p': "Everything happens all at once." 
    }, 
    ReturnValues="UPDATED_NEW" 
) 

Antwort

0

Dies geschieht, weil Sie verschachtelte Eigenschaften von Top-Level-Eigenschaft info versuchen, zu aktualisieren, die noch nicht existiert (OR ist nicht von map type)

Also, bevor Sie dieses Update ausgeführt wird, müssen Sie sicherstellen, dass die Top-Level-Attribut info ist bereits vorhanden.

Oder Sie können einfach die Ausnahme abfangen, wenn es geworfen wird, und führen Sie eine andere Update Erstellen des info Attribut, wie unten dargestellt:

from botocore.exceptions import ClientError 

table = dynamodb.Table('Movies') 

title = "The Big New Movie" 
year = 2015 

try: 
    # Adding new nested attributes `rating` and `plot` 
    # if the top field `info` already exists and is a map 
    response = table.update_item(
     Key={ 
      "year": year, 
      "title": title 
     }, 
     UpdateExpression="set #attrName.rating = :r, #attrName.plot=:p", 
     ExpressionAttributeNames = { 
      "#attrName" : "info" 
     }, 
     ExpressionAttributeValues={ 
      ':r': decimal.Decimal(5.5), 
      ':p': "Everything happens all at once." 
     }, 
     ReturnValues="UPDATED_NEW" 
    ) 

except ClientError as e: 
    if e.response['Error']['Code'] == 'ValidationException': 
     # Creating new top level attribute `info` (with nested props) 
     # if the previous query failed 
     response = table.update_item(
      Key={ 
       "year": year, 
       "title": title 
      }, 
      UpdateExpression="set #attrName = :attrValue", 
      ExpressionAttributeNames = { 
       "#attrName" : "info" 
      }, 
      ExpressionAttributeValues={ 
       ':attrValue': { 
        'rating': decimal.Decimal(5.5), 
        'plot': "Everything happens all at once." 
       } 
      }, 
      ReturnValues="UPDATED_NEW" 
    ) 
    else: 
     raise 
+0

dank ! I löste! –

Verwandte Themen