Project-Books / books-api

GraphQL Books API
https://project-books.github.io/#books-api
MIT License
35 stars 61 forks source link

Database is not seeded correctly (Empty `publisher_book` table) #105

Closed lawynnj closed 3 years ago

lawynnj commented 3 years ago

Describe the bug When the BooksApiApplication is run the database is seeded with some data.

The code that does the seeding: https://github.com/Project-Books/books-api/blob/f44b0d03dad1f2a131acbe14f7439808939be173/src/main/java/com/karankumar/booksapi/BooksApiApplication.java#L45

            Publisher publisher = new Publisher("Bloomsbury");
            publisherRepository.save(publisher);

            Book book1 = createBook(
                    "Harry Potter and the Philosopher's stone",
                    "Philosopher's stone blurb",
                    1997,
                    "9781408810545"
            );
            bookRepository.save(book1);
            publisher.addBook(book1);

The book and publisher tables are seeded correctly, but the publisher_book table is not. When running a select query on the publisher_book table it returns nothing. See below:

mysql> select * from book;
+----+---------------------------+--------+-------+--------+---------------+----------+------------------------------------------+---------------------+
| id | blurb                     | format | genre | isbn10 | isbn13        | language | title                                    | year_of_publication |
+----+---------------------------+--------+-------+--------+---------------+----------+------------------------------------------+---------------------+
|  1 | Philosopher's stone blurb |      2 |    14 | NULL   | 9781408810545 |       10 | Harry Potter and the Philosopher's stone |                1997 |
|  2 | Chamber of secrets blurb  |      2 |    14 | NULL   | 1234567898765 |       10 | Harry Potter and the Chamber of Secrets  |                1998 |
|  3 | Hobbit blurb              |      2 |    14 | NULL   | 1234567898761 |       10 | The Hobbit                               |                1937 |
+----+---------------------------+--------+-------+--------+---------------+----------+------------------------------------------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from publisher;
+----+------------+
| id | name       |
+----+------------+
|  1 | Bloomsbury |
+----+------------+
1 row in set (0.00 sec)

mysql> select * from publisher_book;
Empty set (0.00 sec)

To Reproduce Steps to reproduce the behavior:

  1. Start the application and the database
  2. Access the mysql database
  3. Run select queries on publisher_book: select * from publisher_book;

Expected behavior Running select * from publisher_book; should return the following:

+--------------+---------+
| publisher_id | book_id |
+--------------+---------+
|            1 |       1       |
+--------------+---------+

Desktop (please complete the following information):

Additional context There might be an issue with the model classes and how the relationships are defined.

lawynnj commented 3 years ago

@knjk04 thoughts?

knjk04 commented 3 years ago

@lawynnj Thanks for bringing this up and for the detailed issue description (this made debugging a lot easier)!

You're right that publisher_book should not be empty.

            Publisher publisher = new Publisher("Bloomsbury");
            publisherRepository.save(publisher);

            Book book1 = createBook(
                    "Harry Potter and the Philosopher's stone",
                    "Philosopher's stone blurb",
                    1997,
                    "9781408810545"
            );
            bookRepository.save(book1);
            publisher.addBook(book1);

The issue is that we're saving the publisher before adding the book. I'll create a patch for this shortly