Dynamodb update_item: How to add an element to an attribute of an existing item

Asked

Viewed 12 times

0

Hello, I’m new here in the OS and also with Dynamodb :) - My question should be very simple for the gurus here in the OS, I hope I am not being negligent to the documentation with my question, but I could not understand what I should do to make my code produce the result I wish.

I checked some AWS documentation and also researched some search results for update_item but I’m not succeeding.

What I need to do:

Adicionar um novo elemento em um atributo de um item existente.

Interpretador: Python3.8
Biblioteca: boto3

The table has the following structure:

company=
{
         'email':'', # Minha partition key
         'outros_campos': '',
         'companypartners'=[{
                            'firstname':'',
                            'lastname':'',
                           }],
}

Today I have the following item in the table:

Item = {
         'email': '[email protected]',
         'companypartners'=[{
                            'firstname': 'José',
                            'lastname': 'Silva',
                            }],
        }

Result I hope to achieve:

Item = {
         'email': '[email protected]',
         'companypartners'=[
                           {
                            'firstname': 'José',
                            'lastname': 'Silva',
                            },
                            {
                            'firstname': 'Joao',
                            'lastname': 'Silveria',
                            }
                           ],
        }

I can include a company partner with a update_item, because the attribute already has a zero index element when the item is created (put_item), but when I try to use the same update_item to insert another company (I think it would be a upsert) it replaces the current value of item zero.

This is a little obvious, because the item I’m making update_item is the companypartner[0]. firstname. If I try to change the list index to 1 with companypartner[1]. firstname it gives a very acceptable error, saying that the list item does not exist, or something like that.

So I tried to update_item / ADD, but to no avail. I get the following message:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter AttributeUpdates.Action, value: ADD, type: <class 'str'>, valid types: 
<class 'dict'>
Invalid type for parameter AttributeUpdates.companypartners, value: [{'{"lastname": { "S" 
:Bruno}}',...

Code I’m trying to make work:

def updateaddpartner(v_email, vplname, v_pfname):
        company.update_item(
        Key={
            'email': v_email,
        },
        AttributeUpdates={
            'Action': 'ADD',
            'companypartners': {
                '{"firstname": { "S" :' + v_plname + '}}',
                '{"lastname": { "S" :' + v_pfname + '}}',
            }
        },
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.