From 1b6b71bbdfe90e1c81f8f1d503cd7f65b669b7d8 Mon Sep 17 00:00:00 2001 From: Tejas Khanolkar Date: Tue, 7 Jan 2025 16:03:34 -0800 Subject: [PATCH 1/3] chore: added cascade delete sections in Has One and Has Many data model relationships --- .../data-modeling/relationships/index.mdx | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx index 7e3934edfcf..ad8e2b07fa1 100644 --- a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx @@ -439,6 +439,29 @@ do { +### Handling orphaned foreign keys on parent record deletion in "Has Many" relationship + + + +```ts +// Get the IDs of the related members. +const { data: teamWithMembers } = await client.models.Team.get( + { id: teamId}, + { selectionSet: ["id", "members.*"] }, +); + +// Delete Team +await client.models.Team.delete({ id: teamWithMembers.id }); + +// Delete all members in parallel +await Promise.all( + teamWithMembers.members.map(member => + client.models.Member.delete({ id: member.id }) +)); +``` + + + ## Model a "one-to-one" relationship Create a one-to-one relationship between two models using the `hasOne()` and `belongsTo()` methods. In the example below, a **Customer** has a **Cart** and a *Cart* belongs to a **Customer**. @@ -794,6 +817,26 @@ val cart = Amplify.API.query( +### Handling orphaned foreign keys on parent record deletion in "Has One" relationship + + + +```ts +// Get the customer with their associated cart +const { data: customerWithCart } = await client.models.Customer.get( + { id: customerId }, + { selectionSet: ["id", "activeCart.*"] }, +); + +// Delete Cart if exists +await client.models.Cart.delete({ id: customerWithCart.activeCart.id }); + +// Delete the customer +await client.models.Customer.delete({ id: customerWithCart.id }); +``` + + + ## Model a "many-to-many" relationship In order to create a many-to-many relationship between two models, you have to create a model that serves as a "join table". This "join table" should contain two one-to-many relationships between the two related entities. For example, to model a **Post** that has many **Tags** and a **Tag** has many **Posts**, you'll need to create a new **PostTag** model that represents the relationship between these two entities. From 48510697372fdd5cf8c6a92470bcd6b1788d0f7f Mon Sep 17 00:00:00 2001 From: Tejas Khanolkar Date: Thu, 9 Jan 2025 11:53:03 -0800 Subject: [PATCH 2/3] moved title of section inside ts and js frameworks --- .../data/data-modeling/relationships/index.mdx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx index ad8e2b07fa1..35b02122a22 100644 --- a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx @@ -439,10 +439,10 @@ do { -### Handling orphaned foreign keys on parent record deletion in "Has Many" relationship - +### Handling orphaned foreign keys on parent record deletion in "Has Many" relationship + ```ts // Get the IDs of the related members. const { data: teamWithMembers } = await client.models.Team.get( @@ -817,9 +817,8 @@ val cart = Amplify.API.query( -### Handling orphaned foreign keys on parent record deletion in "Has One" relationship - +### Handling orphaned foreign keys on parent record deletion in "Has One" relationship ```ts // Get the customer with their associated cart From f84bd94f8759ffc511454b61e4748bcad02f15d3 Mon Sep 17 00:00:00 2001 From: Tejas Khanolkar Date: Thu, 9 Jan 2025 15:47:58 -0800 Subject: [PATCH 3/3] nit fix --- .../build-a-backend/data/data-modeling/relationships/index.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx index 35b02122a22..f36b8f92d7d 100644 --- a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx @@ -446,7 +446,7 @@ do { ```ts // Get the IDs of the related members. const { data: teamWithMembers } = await client.models.Team.get( - { id: teamId}, + { id: teamId }, { selectionSet: ["id", "members.*"] }, ); @@ -818,6 +818,7 @@ val cart = Amplify.API.query( + ### Handling orphaned foreign keys on parent record deletion in "Has One" relationship ```ts