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

Getting Dynamodb's ValidationException in Golang

王林
Release: 2024-02-14 21:30:10
forward
1307 people have browsed it

在 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"`
}
Copy after login

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",
}
Copy after login

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
}
Copy after login

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"),
  },
},
Copy after login

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"`
        [...]
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template