Skip to content

Update Android relationship docs to account for cascading delete - #109

Open
richardmcclellan wants to merge 6 commits into
aws-amplify:mainfrom
richardmcclellan:rm/cascading-delete
Open

Update Android relationship docs to account for cascading delete#109
richardmcclellan wants to merge 6 commits into
aws-amplify:mainfrom
richardmcclellan:rm/cascading-delete

Conversation

@richardmcclellan

Copy link
Copy Markdown

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

```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"),

Copy link
Copy Markdown

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?

Copy link
Copy Markdown
Author

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. The Author belongs to the Post, not vice versa. I will remove the "and relationship cleared on Post" part, since that is basically implied.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't Author have one-to-many relationship with Post? That means deleting post will not affect authors. Only if Author gets deleted, then will Post belonging to that specific Author will get deleted.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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?

Example: One post (source model) has one author (target model).

Document describes that Post has-one Author, but the models are the other way around... This should probably be fixed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't Author have one-to-many relationship with Post?

I agree, realistically this would probably be a one-to-many, because an Author would never be restricted to just one Post. I'm actually struggling to think of a good use case for @HasOne at all. I think for now, we can stick with this example.

Document describes that Post has-one Author, but the models are the other way around... This should probably be fixed.

The question is - when deleting a Post, should it delete the Author? (this is how the document reads now) Or when deleting an Author, should it delete the Post (this is what you are proposing). I think either is valid, it just depends on the use case.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

**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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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"?

Amplify.DataStore.delete(article,
deletedArticle -> Log.i("Amplify DataStore", "Article deleted"),
failure -> {});
Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 Where.id() method...

@richardmcclellan richardmcclellan Feb 4, 2021

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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, Publication, so this should actually work?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No adjustment was made to Where.id() yet. I haven't yet found a clean way to implement it.

Comment thread markdown/tests/android/java_relationship.md Outdated
Comment on lines +120 to +125
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) })
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation feels awkward here. Should it be one more level deeper?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated the indentation for these two entire files!

Comment thread markdown/tests/android/kotlin_relationship.md Outdated
Comment thread markdown/tests/android/kotlin_relationship.md Outdated
richardmcclellan and others added 5 commits February 4, 2021 16:03
Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com>
Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com>
Co-authored-by: Raphael Kim <52714340+raphkim@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants