Skip to content

Getting ValidationException for Dynamodb in Golang

0

I've made a schema like this~

    type UserDetails struct {
	UserId       string `json:"userId,omitempty"`
	FirstName    string `json:"firstName,omitempty"`
	LastName     string `json:"lastName,omitempty"`
	Username     string `json:"username,omitempty"`
	HandleName   string `json:"handlename,omitempty"`
	Email        string `json:"email,omitempty"`
	Bio          string `json:"bio,omitempty"`
	Number       int    `json:"phoneNumber,omitempty"`
	SocialHandle string `json:"socialHandle,omitempty"`
	BankDetails  string `json:"bankDetails,omitempty"`
	Image        string `json:"image,omitempty"`
	Password     string `json:"password,omitempty"`
	Resume       string `json:"resume,omitempty"`
	Pincode      int    `json:"pinCode,omitempty"`
	Onboarding   string `json:"onboarding,omitempty"`
}

Here Key and onboarding are my primary and sorting keys respectively. Then I added data like this~

movie := Movie{
	UserId:        "2323",
	Username: "Tdae",
}

Then a normal MarshalMap of the thing I made, and used the data to get the item.

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 weird thing being I inserted data using the same code with few changes, but while fetching data it shows error ~ ValidationException: The provided key element does not match the schema

asked 3 years ago414 views
1 Answer
0
Accepted Answer

This error is likely caused by sending non-key attributes in the GetItem call. When you use MarshalMap, it is including a null value for all other attributes in the key object.

Either you can construct the key manually:

Key: map[string]*dynamodb.AttributeValue{
  "userId": {
    S: aws.String("2323"),
  },
  "username": {
    S: aws.String("The Big New Movie"),
  },
},

Or add omitempty to the struct fields, which will exclude these attributes from the marshalled map when they have no value:

type Movie struct {
	Year         int    `json:"year,omitempty"`
	Title        string `json:"title,omitempty"`
        [...]
}
answered 3 years ago
  • That worked but only with primary key and sort key, If I try to make a call like this I get the same error again ~ movie := structs.UserDetails{ HandleName: "Da", BankDetails: "xs", UserId: "fsd", }

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.