reserved keyword ExpressionAttributeValues in Dyna

2019-02-20 17:09发布

I am able to get a response without the :status : accept expression attribute value but with it, I get the following error when I am using the #status in the projectionExpression line (status is a reserved word in DynamoDB so I had to add hashtag there per https://stackoverflow.com/a/45952329/5921575):

Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" 
UserInfo={__type=com.amazon.coral.validate#ValidationException, 
message=Value provided in ExpressionAttributeValues unused in expressions: keys: {:status}}

Here is code:

queryExpression.keyConditionExpression = "#userId= :userId"
queryExpression.expressionAttributeNames = ["#userId":"userId", "#status":"status"]
queryExpression.expressionAttributeValues = [":userId":userID, ":status":"accept"]
queryExpression.projectionExpression = "#status"

I can go without the ":status":"accept" but I do not want to get a lot of items that do not have the accept value. I can't find an answer in this link or anywhere on stackoverflow: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html

Thanks!

1条回答
叛逆
2楼-- · 2019-02-20 17:27

A bit late, but: Your projection expression should not be "#status" but another word that isn't status. Status is the reserved word, so don't use that for the projection expression. See here for docs on what to do when you need to use a reserved word: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.ReservedWords

userId, on the other hand, does not require a projection expression because it is not a reserved word. See here for a list of reserved words: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

However, you don't need a projection expression. You can simply use the code below. Define "queryExpression.expressionAttributeNames" to create a substitute name for the status attribute value. Here, I used the phrase "statusVal" as a substitute.

Try this. (It worked for me)

let queryExpression = AWSDynamoDBQueryExpression()
queryExpression.expressionAttributeNames = ["#statusVal":"status"] // Using statusVal because it is not reserved. You only need statusVal here because it is the only attribute that also happens to be an AWS reserved word.
queryExpression.keyConditionExpression = "userId = :uId AND #statusVal = :sV"
queryExpression.expressionAttributeValues = [
     ":uId" : String(describing: userId),
     ":sV" : "accept"]

And then perform the operation using AWSDynamoDBObjectMapper! Good luck!

查看更多
登录 后发表回答