在Golang中遇到DynamoDB的ValidationException错误。

huangapple go评论81阅读模式
英文:

Getting ValidationException for Dynamodb in Golang

问题

我已经创建了一个类似这样的模式:

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"`
}

在这里,Key和Onboarding分别是我的主键和排序键。然后我添加了如下的数据:

movie := Movie{
    Key:        "2323",
    Onboarding: "The Big New Movie",
}

然后,我使用这个数据创建了一个普通的MarshalMap,并使用该数据获取了项目。

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
}

奇怪的是,我使用相同的代码插入数据时没有问题,但在获取数据时出现错误:ValidationException: The provided key element does not match the schema。

英文:

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

答案1

得分: 1

这个错误可能是由于在GetItem调用中发送非关键属性引起的。当你使用MarshalMap时,它会在键对象中包含所有其他属性的空值。

你可以手动构建键:

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

或者在结构字段中添加omitempty,当它们没有值时,它将从编组的映射中排除这些属性:

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

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

huangapple
  • 本文由 发表于 2022年12月2日 03:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/74647121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定