I've made a schema 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"`
}
Here Key and onboarding are my primary and sorting keys respectively. Then I added data like this~
movie := Movie{
Key: "2323",
Onboarding: "The Big New Movie",
}
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
CodePudding user response:
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"`
[...]
}