-
Notifications
You must be signed in to change notification settings - Fork 31
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
Update Android relationship docs to account for cascading delete #109
base: main
Are you sure you want to change the base?
Changes from 1 commit
1afad90
b027568
0c5b211
769838c
ab4d21f
98ed0f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,12 +50,22 @@ In one-to-one relationships, if the target model instance is deleted, it will al | |
|
||
```java | ||
Amplify.DataStore.delete(author, | ||
deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), | ||
deleted -> Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post"), | ||
failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); | ||
``` | ||
|
||
In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. | ||
|
||
If the source model is deleted, the target model will be automatically deleted as well. | ||
|
||
```java | ||
Amplify.DataStore.delete(post, | ||
deleted -> Log.i("Amplify DataStore", "Author + Post deleted"), | ||
failure -> Log.e("Amplify DataStore", "Deletion failed", failure)); | ||
``` | ||
|
||
In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. | ||
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. Doesn't 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. Ooooh, I just looked at the original doc, and I think the document is wrong?
Document describes that Post has-one Author, but the models are the other way around... This should probably be fixed. 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.
I agree, realistically this would probably be a one-to-many, because an
The question is - when deleting a 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. Since Post has one Author, your edit is correct! :) My only concern at the moment is that the other parts of the document (not edited in this PR) is currently incorrect. |
||
|
||
:::NEW_COMMAND::: | ||
:::ONE_TO_MANY::: | ||
|
||
|
@@ -104,30 +114,20 @@ Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq(" | |
|
||
**Delete** | ||
|
||
In one-to-many relationships, delete the target model instance first and then delete the source model. | ||
In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally, and on the backend. | ||
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. I personally prefer "parent and child" over "source and target" because I think it's clearer, but I can see the value in keeping a consistent language across different platforms too. What do you think? 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. I like "parent and child" better as well, but consistency across platforms is most important. @renebrandel what do you think about changing this wording across all platforms to "parent and child" instead of "source and target"? |
||
|
||
```java | ||
Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), | ||
matches -> { | ||
while (matches.hasNext()) { | ||
Article article = matches.next(); | ||
Amplify.DataStore.delete(article, | ||
deletedArticle -> Log.i("Amplify DataStore", "Article deleted"), | ||
failure -> {}); | ||
Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), | ||
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. Just something to note: we don't yet have the fix for ambiguity introduced by 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. Hmm, wasn't it fixed by aws-amplify/amplify-android#1133 to default to the class being queried? In this case, 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. No adjustment was made to |
||
matchedPublications -> { | ||
while(matchedPublications.hasNext()) { | ||
richardmcclellan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Publication match = matchedPublications.next(); | ||
Amplify.DataStore.delete(match, | ||
deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted"), | ||
failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); | ||
} | ||
Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"), | ||
matchedPublications -> { | ||
while(matchedPublications.hasNext()) { | ||
Publication match = matchedPublications.next(); | ||
Amplify.DataStore.delete(match, | ||
deleted -> Log.i("Amplify DataStore", "Publication deleted"), | ||
failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)); | ||
} | ||
}, | ||
failure -> {}); | ||
|
||
}, failure -> {} | ||
); | ||
}, | ||
failure -> {}); | ||
|
||
``` | ||
|
||
In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,12 +48,22 @@ In one-to-one relationships, if the target model instance is deleted, it will al | |
|
||
```kt | ||
Amplify.DataStore.delete(newAuthor, | ||
{ Log.i("Amplify DataStore", "Author + Post deleted") }, | ||
{ Log.i("Amplify DataStore", "Author deleted and relationship cleared on Post") }, | ||
{ failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) | ||
``` | ||
|
||
In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted. | ||
|
||
If the source model is deleted, the target model will be automatically deleted as well. | ||
|
||
```kt | ||
Amplify.DataStore.delete(post, | ||
{ Log.i("Amplify DataStore", "Author + Post deleted") }, | ||
{ failure -> Log.e("Amplify DataStore", "Deletion failed", failure) }) | ||
richardmcclellan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
In this example, the `Post` model instance will be deleted and the `Author` model instance will be deleted, locally, and on the backend. | ||
|
||
:::NEW_COMMAND::: | ||
:::ONE_TO_MANY::: | ||
|
||
|
@@ -102,29 +112,19 @@ Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_I | |
|
||
**Delete** | ||
|
||
In one-to-many relationships, delete the target model instance first and then delete the source model. | ||
In one-to-many relationships, deleting the source model will automatically delete all target models that belong to it, both locally, and on the backend. | ||
richardmcclellan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```kt | ||
Amplify.DataStore.query(Article::class.java, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")), | ||
{ matches -> | ||
while (matches.hasNext()) { | ||
val article = matches.next() | ||
Amplify.DataStore.delete(article!!, | ||
{ deletedArticle -> Log.i("Amplify DataStore", "Article deleted") }, | ||
{ failure -> Log.e("Amplify DataStore", "Deletion failed.", failure)}) | ||
} | ||
Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), | ||
{ matchedPublications -> | ||
while (matchedPublications.hasNext()) { | ||
val match: Publication = matchedPublications.next() | ||
Amplify.DataStore.delete(match, | ||
{ deleted -> Log.i("Amplify DataStore", "Publication deleted") }, | ||
{ failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) }) | ||
} | ||
} | ||
) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } | ||
Amplify.DataStore.query(Publication::class.java, Where.id("YOUR_PUBLICATION_ID"), | ||
{ matchedPublications -> | ||
while (matchedPublications.hasNext()) { | ||
val match: Publication = matchedPublications.next() | ||
Amplify.DataStore.delete(match, | ||
{ deleted -> Log.i("Amplify DataStore", "Publication and all related Article instances deleted") }, | ||
{ failure -> Log.e("Amplify DataStore", "Deletion failed.", failure) }) | ||
} | ||
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. indentation feels awkward here. Should it be one more level deeper? 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. I just updated the indentation for these two entire files! |
||
} | ||
) { failure -> Log.e("Amplify DataStore", "Query failed.", failure)} | ||
) { failure -> Log.e("Amplify DataStore", "Query failed.", failure) } | ||
``` | ||
|
||
In this example, the `Publication` with "YOUR_PUBLICATION_ID" and all of its related `Article` model instances will be deleted. | ||
|
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.
I'm kind of confused by this statement. Isn't "Author + Post deleted" more clear?
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.
Well, the
Post
is not deleted. TheAuthor
belongs to thePost
, not vice versa. I will remove the "and relationship cleared on Post" part, since that is basically implied.