□question
Why can i not get item from Dynamodb with partition and sort key?
□context
I am currently making a WebAPI using the Go language.
The app uses DynamoDB as DataStore.
On top of that, when I create a DynamoDB table using AWS CDK and access it from Golang with aws sdk(v2), I get the following error and cannot retrieve it.
I saw that DynamoDB requires partition and sort key to be specified as conditions, so I have specified both, but for some reason it says it doesn't match the schema.
□ErrorMsg
operation error DynamoDB: GetItem, https response error StatusCode: 400, RequestID: J2N511JT7RAG2FCPRUVE2LV9BBVV4KQNSO5AEMVJF66Q9ASUAAJG, api error ValidationException: The provided key element does not match the schema
□Code
・golang(v1.22.4) \
response, err := pr.DB.GetItem(ctx, &dynamodb.GetItemInput{
Key: map[string]types.AttributeValue{
"patientId": &types.AttributeValueMemberN{
Value: strconv.Itoa(patientId),
},
"id": &types.AttributeValueMemberN{
Value: strconv.Itoa(id),
},
},
TableName: aws.String("mask"),
})
・cdk(v2.148.0) \
const pdTable = new TableV2(this, 'mask', {
partitionKey: { name: 'patientId ', type: AttributeType.NUMBER },
sortKey: { name: 'id', type: AttributeType.NUMBER },
globalSecondaryIndexes: [
{
indexName: 'creationDate-index',
partitionKey: { name: 'creationDate ', type: AttributeType.STRING },
},
],
removalPolicy: cdk.RemovalPolicy.DESTROY,
billing: Billing.provisioned({
readCapacity: Capacity.autoscaled({ minCapacity:1,maxCapacity: 10,targetUtilizationPercent:70 }),
writeCapacity: Capacity.autoscaled({ minCapacity:1,maxCapacity: 10,targetUtilizationPercent:70 }),
}),
});
・go.mod \
go 1.22.4
require (
github.com/aws/aws-sdk-go-v2 v1.27.2
github.com/aws/aws-sdk-go-v2/config v1.27.18
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.32.8
github.com/go-chi/chi/v5 v5.0.12
)
require (
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.20.10 // indirect
golang.org/x/net v0.26.0 // indirect
)
require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.1
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.11 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.11 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.12 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/google/uuid v1.6.0
github.com/jmespath/go-jmespath v0.4.0 // indirect
)
□side note
The above error was displayed when the key was not specified sufficiently, so I tried to get the key of the table in question programmatically and the results are as follows.
・code \
tableName := "mask"
input := &dynamodb.DescribeTableInput{
TableName: aws.String(tableName),
}
result, samplerr := pr.DB.DescribeTable(ctx, input)
if samplerr != nil {
fmt.Println("Error describing table:", samplerr)
return nil, imodel.StatusErr{Status: imodel.DBError, Message: "サンプルerror", Err: samplerr}
}
keySchema := result.Table.KeySchema
fmt.Println("Key Schema:", keySchema)
fmt.Println("Key Schema:")
for _, key := range result.Table.KeySchema {
fmt.Printf(" KeyType: %s, AttributeName: %s\n", key.KeyType, aws.ToString(key.AttributeName))
}
・output \
Key Schema:
KeyType: HASH, AttributeName: patientId
KeyType: RANGE, AttributeName: id
I am still learning English, so I may not be able to communicate well, but I would be happy if someone could give me an answer. \
Hi, I'd suggest that you enclose your error messages in block quotes (<> in the menu) to make them more readable.
Hi,Thank you for suggestion!i have fixed it!