Home  >  Article  >  Backend Development  >  Getting Dynamodb's ValidationException in Golang

Getting Dynamodb's ValidationException in Golang

王林
王林forward
2024-02-14 21:30:101229browse

在 Golang 中获取 Dynamodb 的 ValidationException

In Golang, when interacting with Dynamodb, you may sometimes encounter a ValidationException error. This error usually means that the requested data does not comply with the constraints of the Dynamodb table. In this article, we will introduce how to get Dynamodb's ValidationException error in Golang through the guidance of PHP editor Zimo, and provide solutions to handle such errors smoothly. Whether you are a beginner or an experienced developer, this article will help you. Let’s explore how to deal with this common mistake!

Question content

I made a pattern like this~

type movie struct {
    year         int    `json:"year"`
    title        string `json:"title"`
    key          string `json:"userid"`
    email        string `json:"email"`
    bio          string `json:"bio"`
    number       int    `json:"phonenumber"`
    socialhandle string `json:"socialhandle"`
    onboarding   string `json:"username"`
    bankdetails  string `json:"bankdetails"`
    image        string `json:"image"`
    password     string `json:"password"`
    resume       string `json:"resume"`
    pincode      string `json:"pincode"`
}

The key and onboarding here are my primary key and sort key respectively. Then I added the data like this~

movie := movie{
    key:        "2323",
    onboarding: "the big new movie",
}

Then a normal marshalmap of something I made and used the data to get the items.

key, err := dynamodbattribute.MarshalMap(movie)
if err != nil {
    fmt.Println(err.Error())
    return
}

input := &dynamodb.GetItemInput{
    Key:       key,
    TableName: aws.String("tablename"),
}

result, err := svc.GetItem(input)
if err != nil {
    fmt.Println(err)
    fmt.Println(err.Error())
    return
}

The strange thing is that I used the same code to insert the data with almost no changes, but while getting the data it shows the error ~validationexception: The supplied key element does not match the schema

Workaround

This error may be caused by sending non-key attributes in the getitem call. When you use marshalmap, it contains a null value for all other properties in the key object.

You can construct the key manually:

key: map[string]*dynamodb.attributevalue{
  "userid": {
    s: aws.string("2323"),
  },
  "username": {
    s: aws.string("the big new movie"),
  },
},

Alternatively add omitempty to the structure fields, which will exclude these properties from the marshalling map if they have no value:

type Movie struct {
    Year         int    `json:"year,omitempty"`
    Title        string `json:"title,omitempty"`
        [...]
}

The above is the detailed content of Getting Dynamodb's ValidationException in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete