1

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

5
  • Can you share the output of DeacribeTable for your table? Commented Dec 1, 2022 at 19:31
  • @LeeHannigan AttributeDefinitions: [{ AttributeName: "userId", AttributeType: "S" },{ AttributeName: "username", AttributeType: "S" }], even though I've inserted 4-5 data with more names of column Commented Dec 1, 2022 at 19:45
  • Your Movie struct doesn't contain userId or username, so that's why it's complaining. That schema is based on the Primary Key and Sort key you set when you created the table. You probably need to recreate your table with PK/SK matching your Movie schema. Commented Dec 1, 2022 at 22:18
  • @ChrisAnderson movie := Movie{ Key: "23g23", Year: 2015, Title: "The Big New Movie", Onboarding: "Fdsfsd", }, the Movie struct I described consists of the key username and userId, apart from that when I query like this too issue still arises. Commented Dec 5, 2022 at 18:28
  • @ChrisAnderson I think I can only have keys that are present in the schema otherwise the search won't return. Meaning no extra key is allowed. Commented Dec 5, 2022 at 18:40

1 Answer 1

1

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"`
        [...]
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried during the same, i.e, omitempty, but when I when I search with primary key, sort key and any third param,like email or stuff, it throws the same error.
You cannot search for all values in a GetItem request, you must only provide the primary key, made up of Partition and optional Sort keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.