Multiple Inverse Relationships in Master Data

I have an object library that contains two lists of books. It is important that the library supports these two book lists. In my library entity, I have relationships that, from one to many from each list, relate to my book. Similarly, a book has a library relationship. I'm having trouble deleting my data from the database, and I read that I need to set up a reverse relationship to help with data integrity. In this case, however, the book would like to be able to establish feedback with each list in my library entity. How can i do this?

My naive first thought is to implement relationships for both lists. Thus, the book has a relationship of "libraryForList1" and "libraryForList2", so that it can have an inverse for each relationship. I will never have to refer to these properties because, according to the Core Data specification, if I add a book to one of the library lists, it will automatically take care to set the library as the owner of this book.

+4
source share
1 answer

Your "naive first thought" is essentially correct: if your library object has two one-to-many relations with books (let’s say ownedBooks and borrowedBooks for clarity), then your book object will need -reverse inverse relations ( owningLibrary and borrowingLibrary ) and then Core Data will simplify your life.

You may also need to think about delete rules for this relationship: if for some reason the book is deleted, the delete rules for owningLibrary and borrowingLibrary will most likely be Nullify, that is, both libraries will remove the Book from their lists. Removing a library that still has books seems like a bad idea, so perhaps the removal rule for ownedBooks and borrowedBooks should be prohibited: A library cannot be deleted until all books have been accounted for (and removed from the library).

+5
source

All Articles