Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK reject update for item: The provided key element does not match the schema #4238

Closed
3 tasks done
IgorDePaula opened this issue Jan 12, 2022 · 11 comments
Closed
3 tasks done
Assignees
Labels
bug This issue is a bug. closed-for-staleness response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@IgorDePaula
Copy link

Confirm by changing [ ] to [x] below to ensure that it's a bug:

Describe the bug
I have a dynamoDB table with item, and I would like update this item, but I receive error :
"ValidationException: The provided key element does not match the schema
status code: 400, request id: 0D1VIOQSMD3KUQE1PALT2EST8NVV4KQNSO5AEXXXXXXXXXXXXXxxxxxx"

Version of AWS SDK for Go?
SDK v1.42.32

Version of Go (go version)?
Go 1.6

To Reproduce (observed behavior)

package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
	"os"
)

type Item struct {
	Id    string `json:"id"`
	Year  int    `json:"year"`
	Title string `json:"title"`
	Plot  string `json:"plot"`
}

func main() {
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))

	// Create DynamoDB client
	svc := dynamodb.New(sess)

	
	newItem := Item{
		Year:  2021,
		Title: "The Big New Movie or not",
		Plot:  "Nothing happens at all.",
	}
	av, err := dynamodbattribute.MarshalMap(newItem)
	if err != nil {
		fmt.Println("Got error marshalling new movie item:")
		fmt.Println(err.Error())
		os.Exit(1)
	}
	newItemKey := Item{
		Id: "5",
	}
	tableName := "movies"

	
	newInput := &dynamodb.UpdateItemInput{
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":id": {
				S: aws.String(newItemKey.Id),
			},
			
		},
		TableName:        aws.String(tableName),
		Key:              av,
		ReturnValues:     aws.String("UPDATED_NEW"),
		UpdateExpression: aws.String("set id = :id"),
	}

	_, err = svc.UpdateItem(newInput)
	if err != nil {
		fmt.Println("Got error calling PutItem:")
		fmt.Println(err.Error())
		os.Exit(1)
	}

}

Expected behavior
I expect update the item

@IgorDePaula IgorDePaula added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 12, 2022
@IgorDePaula
Copy link
Author

Additional context
The table has a global index too.

@IgorDePaula
Copy link
Author

I got, but I received another erro not mapped/documented:

Got error calling PutItem:
ValidationException: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: year
        status code: 400, request id: M123RCPG5DFSAH4UC7KIRVDL0BXXXXXXXXXXXXXXXXXXXXXXXXXX
exit status 1
igor@developer:~/projetos/GoDynamoDB$ go run main.go
Got error calling PutItem:
ValidationException: Value provided in ExpressionAttributeValues unused in expressions: keys: {:year}
        status code: 400, request id: 8MJI6USV5K1FEQCJ7NHHE424SVVVXXXXXXXXXXXXXXXXXXXXXXXXXXX
exit status 1

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@IgorDePaula
Copy link
Author

The final code is:

package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"os"
)

type Item struct {
	Id    string `json:"id"`
	Year  int    `json:"year"`
	Title string `json:"title"`
	Plot  string `json:"plot"`
}

func main() {
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))

	// Create DynamoDB client
	svc := dynamodb.New(sess)

	
	newItem := Item{
		Year:  2021,
		Title: "The Big New Movie or not",
		Plot:  "Nothing happens at all.",
	}
	
	newItemKey := Item{
		Id: "5",
	}
	
	tableName := "movies"

	
	newInput := &dynamodb.UpdateItemInput{
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":title": {
				S: aws.String(newItem.Title),
			},
			":plot": {
				S: aws.String(newItem.Plot),
			},
		},
		Key: map[string]*dynamodb.AttributeValue{
			"id": {
				S: aws.String(newItemKey.Id),
			},
		},
		ReturnValues:     aws.String("UPDATED_NEW"),
		TableName:        aws.String(tableName),
		UpdateExpression: aws.String("set title = :title,  plot = :plot"),
	}

	_, err := svc.UpdateItem(newInput)
	if err != nil {
		fmt.Println("Got error calling PutItem:")
		fmt.Println(err.Error())
		os.Exit(1)
	}

}

(I reopened the issue for notice by github)

@IgorDePaula IgorDePaula reopened this Jan 12, 2022
@rittneje
Copy link
Contributor

@IgorDePaula What are the partition key and sort key for your table?

@diogoalexandria
Copy link

titleValue := dynamodb.AttributeValue {
                S : aws.String(newItem.Title),
}

plotValue := dynamodb.AttributeValue {
                S : aws.String(newItem.Plot),
}

idValue := dynamodb.AttributeValue {
                S: aws.String(newItemKey.Id),
}

newInput := &dynamodb.UpdateItemInput{
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":title": &titleValue,
			":plot": &plotValue,
		},
		Key: map[string]*dynamodb.AttributeValue{
			"id": &idValue,
		},
		ReturnValues:     aws.String("UPDATED_NEW"),
		TableName:        aws.String(tableName),
		UpdateExpression: aws.String("set title = :title,  plot = :plot"),
	}

Worked for me

@vudh1 vudh1 self-assigned this Apr 15, 2022
@vudh1
Copy link
Contributor

vudh1 commented Apr 20, 2022

Hi, is this issue still persisting with the latest version of SDK?

@vudh1 vudh1 added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Apr 20, 2022
@github-actions
Copy link

This issue has not received a response in 1 week. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Apr 23, 2022
@mateustalles
Copy link

Im having that issue in @aws-sdk/client-dynamodb for NodeJS. Im trying to update an Item and no matter if I input the hash and range key correctly, it still says that the provided key element doesn't match the schema.

@user1689
Copy link

Same issue when using @aws-sdk/client-dynamodb for Java

@user1689
Copy link

user1689 commented Dec 18, 2023

Im having that issue in @aws-sdk/client-dynamodb for NodeJS. Im trying to update an Item and no matter if I input the hash and range key correctly, it still says that the provided key element doesn't match the schema.

Did u solve it now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closed-for-staleness response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

6 participants