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

Conditional Check for custom primary key

Open Gl0ntz opened this issue 6 years ago • 5 comments

Is your feature request related to a problem? Please describe.

I am not sure if this is a known thing. I searched for similar open issues haven't found any. When I set a custom primary key for a model like this example :

type Project @model @key(fields:["projectId"]) {
     projectId:ID!
     projectName:String
}

When I run the auto generated CreateProject Mutation there is no conditional check for the primary key projectId. Instead it overwrites any existing items with the same primary key.

Describe the solution you'd like

Since we are describing the @key directive, I am assuming the CLI can do this automatically while it provisions the resolvers? Also what will be the behaviour for compound primary keys with hash keys and sort keys?

Describe alternatives you've considered

I was able to solve my issue by editing the resolver for Mutation create Project and adding

#set( $condition = {
  "expression": "attribute_not_exists(#projectId)",
  "expressionNames": {
      "#projectId": "projectId"
 }})

Gl0ntz avatar Feb 19 '20 08:02 Gl0ntz

@ammarkarachi enhancement? Seems like a bug that graphql-dynamodb-transformer functionality that fails to account for the primary key not always being 'id'. Its not documented for the user that changing the pk name comes at the cost of broken features.

RossWilliams avatar Feb 19 '20 20:02 RossWilliams

@RossWilliams you are right it should be treated as a bug

ammarkarachi avatar Feb 19 '20 23:02 ammarkarachi

I've just come across the same issue.

It looks the logic to check for a custom primary key using ModelObjectKey already exists in the makeUpdateResolver method.

Perhaps the same or similar logic, swapping attribute_exists for attribute_not_exists, could be applied to where the primary key is hard coded to id in the makeCreateResolver method?

bdunks avatar Aug 17 '20 15:08 bdunks

Closing in favor for #7261

attilah avatar May 07 '21 22:05 attilah

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 Jun 06 '21 23:06 github-actions[bot]