DynamoDB: ValidationException: ExpressionAttributeNames can only be specified when using expressions

0

I am writing some code to query Dynamo with a certain filter and I'm using the expression package in the Go sdk:

The variable project that is referred to is defined in another section of the code. Its string properties owner and name are not nil/empty.

dynamo := dynamodb.New(sess)

filt := expression.Name("projectOwner").Equal(
	expression.Value(project.owner),
).And(
	expression.Name("projectName").Equal(
		expression.Value(project.name),
	),
)

expr, err := expression.NewBuilder().WithFilter(filt).Build()
if err != nil {
       panic(err)
}

queryInput := dynamodb.QueryInput{
      KeyConditionExpression:    expr.KeyCondition(),
      ProjectionExpression:      expr.Projection(),
      ExpressionAttributeNames:  expr.Names(),
      ExpressionAttributeValues: expr.Values(),
      TableName: aws.String("projects"),
}
	
out, err := dynamo.Query(&queryInput)
if err != nil {
       panic(err)
}

I get this error:

panic: ValidationException: ExpressionAttributeNames can only be specified when using expressions

I don't know where it's from or what it means: I am using expressions.

asked 2 years ago4791 views
1 Answer
0

You are confusing between the syntax of Query and Scan.
When building a Query command you should use WithKeyCondition(KeyCond):

builder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(proj)

and when building a Scan you should use WithFilter(filt):

expr, err := expression.NewBuilder().WithFilter(filt).WithProjection(proj).Build()
MLGuy
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions