3

I have a data model that consist in three models Profile, Company, Profession. Profile is a model that I am using in order to link a Cognito user with structured data (such as Company and Profession). Profile has a many-to-many relation with Company and Profession.

When we go to towards the AWS documentation it is clear that we need to create models and then create a new record on the join table amplify generates when we talk about many-to-many relations.

Nonetheless, the link above doesn't provide information to delete or update many-to-many relations. I assume that in order to update I need to delete the entry on the Join table and then add the new entries (so I avoid duplicated entries and make the job done)

But when I try to delete an entry from the join table I receive the following error: Error of type "ConditionalCheckFailedException"

Going into the glossary of errors on DynamoDB I don't see which condition I may be created from my side unintended as the error suggest my condition is failing

In summary, in order to allow users to manipulate the professions on their profiles, I am trying to:

  • Add a new entry to the ProfileProfession table, and it works

But when I try to update by:

  • Delete previous entries -> This fails with the error above
  • Add new entries -> This works

In case of more context needed here a snippet of the important code I have: https://gist.github.com/duranmla/a8caf14f61ba25fd30610cdb470ee58f

Perhaps my code is not great but I just want to understand how this things work before move further. Thanks in advance to everyone for taking the time to read. ❤️

Alexis Duran
  • 620
  • 14
  • 28

1 Answers1

1

The answer was:

  • Yes, for many-to-many relations you need to manipulate the join Table
  • Yes, to update you need to remove or add entries to the join table

Also, the unexpected behaviour one get by seeing and then not seeing data that is persisted in DynamoDB, or Error ConditionalCheckFailedException one need to:

  • Check policies applied to the model
  • Understand how DataStore subscription works (I am currently DataStore.clear() on logout and DataStore.start() on signIn)

In my specific case once I did the following:

  • Create a Profile record on post user confirmation to tie the Cognito User to all other models I will have on Amplify Data Model
  • Update the authorization Policies on my Profile model (check image below)
  • Clean local data on user logout using DataStore.clear()
  • Start a subscription when user signIn using DataStore.start()

I was able to make things work as expected


An image of how the policies look like for Profile: https://i.stack.imgur.com/aqXxk.png

Allow any signed-in users authenticated with Cognito User Pool can Read, Update, and Delete Profile and Enable owner authorization while deny non owners to Update or Delete

Good resources to check:

Alexis Duran
  • 620
  • 14
  • 28