DynamoDB “提供的键元素与模式不匹配错误”

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

DynamoDB "The provided key element does not match the schema error"

问题

我已经在terraform中定义了dynamodb表格:

name                   = "component_table"
partition_key          = "name"
partition_key_type     = "S"
sort_key               = "version"
billing_mode           = "PAY_PER_REQUEST"
environment            = "testing"
aws_region             = "eu-west-1"
restore_to_latest_time = true
point_in_time_recovery = true
extra_attributes = [
    {
      name = "version"
      type = "S"
    },
    {
      name = "team"
      type = "S"
    }
]
local_secondary_index = [
{
  name            = "TeamSecondaryIndex"
  projection_type = "ALL"
  range_key       = "team"
}
]

我使用aws操作符GetItem进行了查询,我将name定义为abc以进行测试,但似乎无法查询GetItem函数,因为它显示了以下错误:

提供的键元素与模式不匹配

我运行的函数如下:

func (m *Manager) ComponentGetByName(ctx context.Context, name string) (componentModel.Component, error) {

getItemInput := &awsDDB.GetItemInput{
    TableName: &m.cfg.tableName,
    Key: map[string]types.AttributeValue{
        "name": &types.AttributeValueMemberS{
            Value: name,
        },
    },
}
getItemOutput, err := m.Client.GetItem(ctx, getItemInput)
return getItemOutput
}

出于保密原因,我省略了将getItemOutput转换为组件模型的一些细节。我想知道为什么会出现这种情况,以及如何解决它?欢迎讨论。谢谢!

英文:

I've defined the dynamodb table in terraform here:

name                   = "component_table"
  partition_key          = "name"
  partition_key_type     = "S"
  sort_key               = "version"
  billing_mode           = "PAY_PER_REQUEST"
  environment            = "testing"
  aws_region             = "eu-west-1"
  restore_to_latest_time = true
  point_in_time_recovery = true
  extra_attributes = [
    {
      name = "version"
      type = "S"
    },
    {
      name = "team"
      type = "S"
    }
]
local_secondary_index = [
{
  name            = "TeamSecondaryIndex"
  projection_type = "ALL"
  range_key       = "team"
}
]

I've queried it with aws operator GetItem, I've defined name to be abc for testing purposes but can't seem to query a GetItem function as it says

The provided key element does not match the schema error

The function I ran is this:

func (m *Manager) ComponentGetByName(ctx context.Context, name string) (componentModel.Component, error) {

getItemInput := &awsDDB.GetItemInput{
    		TableName: &m.cfg.tableName,
    		Key: map[string]types.AttributeValue{
    			"name": &types.AttributeValueMemberS{
    				Value: name,
    			},
    		},
    	}
    	getItemOutput, err := m.Client.GetItem(ctx, getItemInput)
return getItemOutput
}

I've left some details out such as converting getItemOutput into a component model for secrecy. I'm wondering why this happens and how can I resolve it? Open for a discussion. Thanks!

答案1

得分: 2

问题是你正在尝试仅使用分区键值进行 GetItem 操作。要执行 GetItem 操作,你必须提供完整的主键,即分区键和排序键的值。在你的情况下,nameversion 是必需的。

如果你只知道分区键,那么你必须执行 Query 操作。

你必须提供分区键属性的名称和该属性的单个值。Query 操作将返回具有该分区键值的所有项。

英文:

The issue is you are trying to do a GetItem using only the partition key value. To do a GetItem you must provide the full primary key which is Partition and sort key values. In your case name and version are required.

If you only know the Partition key, they you must perform Query operation.

You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value

huangapple
  • 本文由 发表于 2023年4月19日 19:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053984.html
匿名

发表评论

匿名网友

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

确定