Reputation: 4596
I have fields below in dynamo dB table
event_on -- string type
user_id -- number type
event name -- string type
Since this table may have multiple records for user_id and event_on is the single field which can be unique so I made it primary key and user_id as sort key
Now I want to delete the all records of a user, so My code is
response = dynamodb.delete_item(
TableName=events,
Key={
"user_id": {"N": str(userId)}
})
It throwing error
Exception occured An error occurred (ValidationException) when calling the DeleteItem operation: The provided key element does not match the schema
also is there anyway to delete with range
Can someone suggest me what should I have do with dynamodb table structure to make this code work
Thanks,
Upvotes: 4
Views: 15450
Reputation: 5747
It sounds like you've modeled your data using a composite primary key, which means you have both a partition key and a sort key. Here's an example of what that looks like with some sample data.
In DynamoDB, the most efficient way to access items (aka "rows" in RDBMS language) is by specifying either the full primary key (getItem) or the partition key (query). If you want to search by any other attribute, you'll need to use the scan operation. Be very careful with scan
, since it can be a costly way (both in performance and money) to access your data.
When it comes to deletion, you have a few options.
In order to effectively use any of these options, you'll first need to identify which items you want to delete. Because you want to fetch using the value of the sort key alone, you have two options;
scan
to find the items of interest. This is not ideal but is an option if you cannot change your data model.user_id
.If you choose option 2, your data would look like this
This would allow you to fetch all item for a given user, which you could then delete using one of the methods I outlined above.
Upvotes: 9
Reputation: 2545
If you are created a DynamoDB table by the Primary key and sort key, you should provide both values to remove items from that table. If the sort key was not added to the primary key on the table creation process, the record can be removed by the Primary key.
How I solved it.
Actually, I tried to not add the sort key when created the table. And I'm using indexes for sorting and getting items.
Upvotes: 0
Reputation: 6998
As you can see here, delete_item
needs the primary key and not the sort key. You would have to do a full scan, and delete everything that contains the given sort key.
Upvotes: 2