amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Error when providing _version attribute on updates when key attribute is defined in schema

Open dysan1257 opened this issue 6 years ago • 4 comments

On a table that's backed by a versioned DynamoDB table (to be used with datastore) when we run GraphQL mutations via the appsync console we can't seem to make any updates.

  • If we supply the _version attribute we get a "Updating _version field can not be set from the client" error.
  • If we run a mutation without providing the _version attribute the _version attribute on the record increments but no data is updated.

I believe I tracked the issue down to us having a @key("id") attribute in the amplify api graphql schema. We had originally had wanted to use a custom partition and sort key but that's also not possible so I changed it to just "id".

If we remove the @key("id") attribute things work correctly and the generated dynamoDB schema is the same. Is it currently not supported to have any definition of the key when using versioned sources?

This works:

type Note2 @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  note: String
  owner: String
}

This does not work:

type Note3 @model @key(fields: ["id"]) @auth(rules: [{ allow: owner }]) {
  id: ID!
  note: String
  owner: String
}

dysan1257 avatar May 22 '20 18:05 dysan1257

This looks to be specific to Amplify CLI, I have transferred this to their repo.

sammartinez avatar May 22 '20 20:05 sammartinez

I have the same issue with a model with the following key type Notifications @key(fields: ["key"])

What I found after some digging is that the problem is in the resolver Mutation.updateNotification.req.vtl that is generated for the model. Since the model has a custom key it adds this in the top of the file:

#set( $modelObjectKey = {
  "key": $util.dynamodb.toDynamoDB($ctx.args.input.key)
} )

Later when it is identifying the key fields it creates the bug:

#if( $modelObjectKey )
  #set( $keyFields = [] )
  #foreach( $entry in $modelObjectKey.entrySet() )
    $util.qr($keyFields.add("$entry.key"))
  #end
#else
  #set( $keyFields = ["id", "_version", "_deleted", "_lastChangedAt"] )
#end

This cause the $keyFields to only contain ["key"], instead of ["key", "_version", "_deleted", "_lastChangedAt"]. This again causes that it tries to update the _version field instead of just check the version for it.

I solved it now by copying the Mutation.updateNotification.req.vtl to the custom resolver folder and changed the #if( $modelObjectKey ) part to:

#if( $modelObjectKey )
  #set( $keyFields = ["_version", "_deleted", "_lastChangedAt"] )
  #foreach( $entry in $modelObjectKey.entrySet() )
    $util.qr($keyFields.add("$entry.key"))
  #end
#else
  #set( $keyFields = ["id", "_version", "_deleted", "_lastChangedAt"] )
#end

After this change, it seems to work fine

atlesp avatar Jun 11 '20 10:06 atlesp

@UnleashedMind I see that is marked as fixed. This is a pretty big issue. When are we going to use the fixed version?

I executed the following:

npm install -g @aws-amplify/cli@latest

And if the problem are in the resolver... is there any command to regenerate those?

marianolc avatar Sep 04 '20 18:09 marianolc

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 25 '21 22:05 github-actions[bot]