Skip to content

Commit

Permalink
chore: added cascade delete sections in Has One and Has Many data mod… (
Browse files Browse the repository at this point in the history
#8189)

* chore: added cascade delete sections in Has One and Has Many data model relationships

* moved title of section inside ts and js frameworks

* nit fix
  • Loading branch information
tejas2008 authored Jan 10, 2025
1 parent b9b4e81 commit 685b786
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,29 @@ do {

</InlineFilter>

<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue"]}>

### 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 })
));
```

</InlineFilter>

## 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**.
Expand Down Expand Up @@ -794,6 +817,26 @@ val cart = Amplify.API.query(

</InlineFilter>

<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue"]}>

### 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 });
```

</InlineFilter>


## 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.
Expand Down

0 comments on commit 685b786

Please sign in to comment.