Conditional Check for custom primary key
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"
}})
@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 you are right it should be treated as a bug
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?
Closing in favor for #7261
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.