2015-12-24 11 views
9

Seguendo the documentation, sto cercando di creare una dichiarazione di aggiornamento che aggiorni o aggiunga se non esiste un solo attributo in una tabella dynamodb.Esempio di update_item in dynamodb boto3

sto cercando questo

response = table.update_item(
    Key={'ReleaseNumber': '1.0.179'}, 
    UpdateExpression='SET', 
    ConditionExpression='Attr(\'ReleaseNumber\').eq(\'1.0.179\')', 
    ExpressionAttributeNames={'attr1': 'val1'}, 
    ExpressionAttributeValues={'val1': 'false'} 
) 

L'errore che sto ottenendo è:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeNames contains invalid key: Syntax error; key: "attr1"

se qualcuno ha fatto qualcosa di simile a quello che sto cercando di realizzare si prega di condividere esempio .

risposta

13

Trovato esempio operativo here, molto importante elencare come Chiavi tutti gli indici della tabella, ciò richiederà una query aggiuntiva prima dell'aggiornamento, ma funziona.

response = table.update_item(
    Key={ 
     'ReleaseNumber': releaseNumber, 
     'Timestamp': result[0]['Timestamp'] 
    }, 
    UpdateExpression="set Sanity = :r", 
    ExpressionAttributeValues={ 
     ':r': 'false', 
    }, 
    ReturnValues="UPDATED_NEW" 
) 
+0

Va notato che il vostro errore originale si riferiva alla ExpressionAttributeNames che è stata esclusa dal campione fornite in questa risposta .... mentre si ha a includere tutti i valori per la chiave per aggiornare una voce presente non era direttamente l'errore che avevi in ​​origine. – James