Closed knjk04 closed 2 years ago
Hi I would like to work on this issue
Sure, thanks!
@Michaelg2021 How are you getting on with this?
I'll have it done by the end of the week. Got my progress held up by school work last week, my apologies.
@Michaelg2021 Do you have an update on this?
@Michaelg2021 Unassigning due to no response
Hi, can I grab this one?
Hi @Syemon, sure. Thanks!
@knjk04 I see that a book can have multiple authors. Should I delete book only if there are no relations with authors left?
@Syemon Yes, good idea!
@knjk04 I have problems with deleting books with only one relation. I have tried this approach.
public void deleteAuthor(@NonNull Author author) {
Set<Book> booksWithOneAuthor = author.getBooks()
.stream()
.filter(book -> book.getAuthors().size() == 1)
.collect(Collectors.toSet());
Set<Book> books = author.getBooks();
books.forEach(author::removeBook);
bookRepository.deleteAll(booksWithOneAuthor);
authorRepository.deleteById(author.getId());
}
And it appears that records of book_author table (the ones with two authors) were not cleared Cannot delete or update a parent row: a foreign key constraint fails (`booksapi`.`book_author`, CONSTRAINT `FKbjqhp85wjv8vpr0beygh6jsgo` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`))
Can I take an approach where I would delete such relations in book_author using native query?
Add a mutation in
AuthorMutation
that deletes the Author (specified by its ID) and all of its books.removeBooks()
should be used