Sleneau
Sleneau

Reputation: 31

AWS Dynamodb boto3 batch_write_item isn't working

I've been trying to wrap my mind around how batch_write_item works in the boto3 (Python SDK for AWS). My code is as follows:

users = self.scan_table(filterKey="key",filterValue="value",table="users")
deleteUsers = []

# Create lists to delete
for u in users:
    deleteUsers.append({"DeleteRequest":{"Key":{"S":u["user_id"]}}})

# Delete items
ret = self.db.batch_write_item(
    RequestItems={
        "users":delUsers
    }
)

The output is as follows (for each item to be deleted):

ClientError: An error occurred (ValidationException) when calling the
BatchWriteItem operation: The provided key element does not match the schema

As far as I can tell, I am following the prescribed instructions in the documentation exactly. What am I missing?

EDIT: As per jarmod's comment, I felt I needed to specify that I am indeed using the boto3 resource, not the client. There are two separate sets of documentation for the client vs. the resource. Turns out that using the correct documentation for the function you are using is advisable.

Upvotes: 1

Views: 3062

Answers (1)

Sleneau
Sleneau

Reputation: 31

Oh, it turns out I wasn't reading the documentation correctly (though it could stand to be a lot clearer in places). I changed the following code:

    # Create lists to delete
    for u in users:
        delUsers.append({"DeleteRequest":{"Key":{"S":u["user_id"]}}})

To:

    # Create lists to delete
    for u in users:
        delUsers.append({"DeleteRequest":{"Key":{"user_id":u["user_id"]}}})

The explanation is that I assumed that "S" was to designate the type of the key, but that isn't what's being asked for. After playing around removing the brackets, and playing around with dummy data, it became clear that I needed to change "S" to the actual name of the primary key of the table I was trying to effect, in this case, "user_id".

It worked just fine after that.

Upvotes: 2

Related Questions