DynamoDB UpdateItem error

  • Goal: We are trying to build a quick admin UI to CRUD a DynamoDB table. We are trying to UpdateItem and receiving an error

"The provided key element does not match the schema"

  • Steps: We built a table and returning data to the table. We have built a Edit Form via a modal to edit the selected row. The Edit form displays the correct data for the row.

However, when the user submits the form, the error occurs and the item is not updated.

The key for the table is just the camera_id

DynamoDB key information

Sorry i copied and pasted the wrong screen shot of the config.

Thank you again for the suggestions
Greg

Missing attribute names and an SK for your Key.

This post got longer than I intended, but early on when we switched to Dynamo, we couldn't get much help. I don't think many people here use it honestly, or they don't have questions about it. I thought I would throw a little extra into my response. You can use these actual working queries to troubleshoot your issues. NoSQL Workbench was a huge help to us getting Dynamo to work with Retool.

I highly suggest storing these types of queries in a regular JavaScript query and calling them dynamically. This approach cuts down on resources and makes troubleshooting and ordering queries much easier.

For example, my "Put" or "Update" actions are all in one DynamoDB query called "singlePut", and I point a temporary state at it, changing the temp state to whatever query I'm running.

Inside the standard query, I have a switch statement that looks at a temp state that I set before triggering the query based on which button or action is triggering the query. So, when I click the button, it sets the putOption temp state to one of the cases contained in the below code and runs whichever case is needed based on the button press. The first case is an error if somehow the temp state didn't get set, which I added after failing to set the temp state during early testing and getting so frustrated that I added that in so if I forgot again, I wouldn't spend so much time troubleshooting.

switch (putOption.value) {
////////////////////////////////////////////////      
case "": utils.showNotification({title:"Something went wrong, contact support",description:"ERR#2DPQ",notificationType:"info"})
break;
////////////////////////////////////////////////
case "newDiscussion":
var discussionUUID = uuid.v4()
await putOption.setValue(
{
  TableName:"TABLE_NAME",
  Item:{
  PK:"Discussion#"+discussionUUID,
  SK:"Discussion#"+discussionUUID,
  subject:textInput3.value,
  linked:checkbox1.value,
  users:multiselect1.value,
  latestMessage:textArea1.value,
  latestUser:current_user.email,
  searchSK:new Date(),
  searchPK:"Discussion",
  gsi1PK:false,  
  gsi1SK:select1.value
}})
await singlePut.trigger()
await selectedDiscussion.setValue("Discussion#"+discussionUUID)
await putOption.setValue("newMessage")
console.log("Put Option set to "+putOption.value)
putQuerys.trigger()
console.log("Recusion")
getDiscussions.trigger()
break;
////////////////////////////////////////////////  
case "newMessage":
console.log("Recursion Success")
console.log(selectedDiscussion.value)
await putOption.setValue(
{
TableName:"TABLE_NAME",
Item:{
PK:selectedDiscussion.value,
SK:"Message#"+uuid.v4(),
user:current_user.fullName,
message:textArea1.value,
linkedItem:newMessagePDFPreview.value,
image:newMessageImagePreview.value,
searchPK:selectedDiscussion.value,
searchSK:new Date()
}})
singlePut.trigger()
textInput3.clearValue()
select1.clearValue()
multiselect1.clearValue()
textArea1.clearValue()
console.log("Message followed up")
console.log(putOption.value)
getSelectedDiscussion.trigger()
    break;
/////////////////////////////////////////////
case "soloMessage":
console.log("Solo Message")
await putOption.setValue(
{
TableName:"TABLE_NAME",
Item:{
PK:selectedDiscussion.value,
SK:"Message#"+uuid.v4(),
user:current_user.fullName,
message:textInput4.value,
linkedItem:newMessagePDFPreview.value,
image:newMessageImagePreview.value,
searchPK:selectedDiscussion.value,
searchSK:new Date()
}})
await singlePut.trigger()
textInput4.clearValue()
newMessageImagePreview.setValue("")
newMessagePDFPreview.setValue("")
console.log("Solo Message Done")
getSelectedDiscussion.trigger()
break;
}

This might not be the best way to handle any of this, but I've found it very useful for being able to test our code. The above are just standard puts into our DynamoDB, but below is an update query that doesn’t use the visual parameters.

{
    "TableName": "TABLE_NAME",
    "Key": {
      "PK":"project#001",
      "SK":"openings"
    },
    "UpdateExpression": "SET #doorTypes.#type =if_not_exists(#doorTypes.#type,:type)",
    "ExpressionAttributeValues": { ":type": {{transPutDT.value.putObj}} },
    "ExpressionAttributeNames": {"#doorTypes": "doorTypes","#type":{{tiDoorType.value}} },
  }

You can see from the above you can also drill down into objects this way, probably would work in the visual parameter version also, but it's easier this way, I think.

I highly suggest downloading the NoSQL workbench and connecting to your Dynamo instance; you can design queries in the operation builder and output them into Python. It isn’t perfect, but it gives you a great starting point on a very complicated update.


image

You basically just need to remove the "S" in the ExpressionAttributeValues portion or anywhere where it tries to define a specific value type.

GOOD LUCK!

PS: This was from an early test, my code is much prettier than that first block now. I apologize if its hard to read hahaha.

1 Like

TLDR:

Create expression attribute names like so {"#camera_id": "camera_id", "#agent_uid": "agent_uid"} (and so on).

Match the update expression to use #camera_id instead of camera_id, and from what I can tell, you should be good to go if you have no SK on the key. If you have an SK then add that into the key also.

1 Like

WOW!! Thank you so much for the detailed explaination.

Will try this out and report back shortly!

Huge shout out. Your suggestions were extremely helpful. I was able to work out my issues!

Thanks again
Greg

1 Like

No problem, if you run into any more issues with Dynamo tag me in the forum post and I'll try to assist the best I can.

1 Like