-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: added cascade delete sections in Has One and Has Many data mod… #8189
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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**. | ||||||||
|
@@ -794,6 +817,25 @@ 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||
|
||||||||
```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. | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: