Home > Blockchain >  PartiQL INSERT Statement to append a Value to a List Attribute
PartiQL INSERT Statement to append a Value to a List Attribute

Time:01-14

I am trying to append a Value to an existing List Attribute in DynamoDB using PartiQL Syntax.

The Primary Key of the Item consists of:

pk = userId
sk = happeningId
table_name = test_table
userId = "user09hfh47egd53tgd"
happeningId = "happ09hdg2536dget7354tdg"
contactId = "C0003"
decision = "accept"
if decision == "accept":

    stmt = f"UPDATE \"{table_name}\" SET accept = list_append(accept, :'{contactId}') WHERE pk='{userId}' AND sk='{happeningId}'"
    print(stmt)
    resp = dynamodb_client.execute_statement( Statement=stmt )
else:
    stmt = f"UPDATE \"{table_name}\" SET cancel = list_append(cancel, :'{contactId}') WHERE pk='{userId}' AND sk='{happeningId}'"
    print(stmt)
    resp = dynamodb_client.execute_statement( Statement=stmt )

The final string looks like this

UPDATE "test_table" SET accept = list_append(accept, :'C0003') WHERE pk='user09hfh47egd53tgd' AND sk='happ09hdg2536dget7354tdg'

When i try to run my code i get the following error message:

ClientError: An error occurred (ValidationException) when calling the ExecuteStatement operation: Statement wasn't well formed, can't be processed: Unexpected term

Anyone has an idea what i am doing wrong?

CodePudding user response:

Try to write your Statement like that:

contactId = ["C0003"]

 stmt = f"UPDATE \"{table_name}\" SET accept = list_append(accept, {contactId}) WHERE pk='{userId}' AND sk='{happeningId}'"

Explanation: The Value that you want to append to your List, needs to come in a List Type as well. Furthermore the doublepoint was not necessary and the quotes in '{contactId}' need to be removed because they transform the inserted value into a string. And as i said, the Value needs to be as List Type as well.

Hope that helps.

  • Related