Search code examples
databaserelational-databaserelationshipeloquent-relationship

How would you model this in a relational database?


I have three tables: Books, Sagas and Universes. Books has idSaga as foreign key. Sagas has idUniverse as foreign key.

All of them are one to Many relationships.

A book can be an installment of a saga and that saga part of a bigger universe; for example: the lord of the rings and the fellowship of the ring is the first installment of the LOTR saga, and the LOTR is in the Legendarium universe.

How should i address a book like The hobbit? it isn't part of any saga but is part of the Legendarium universe.

I don't want to add a new field to the table Books (for the foreign key), it looks like breaking normalization. Adding a new table and relationships between books and universe looks a bit excesive.


Solution

  • I don't want to add a new field to the table Books (for the foreign key), it looks like breaking normalization.

    This is exactly what I would do. I'm not sure what you mean by "it looks like breaking normalization", but to me this seems ideal because the Book -> Universe relationship could be useful.

    For example, let's say you had a new feature where you wanted to count all the books in a given universe. If you don't have a foreign key in the books table, you'd have to query the sagas table for information that is in no way related to sagas.

    By excluding this foreign key, you are limiting your options for using this relationship in the future.

    In summary, I would go:

    Books M:1 Saga
    Books M:1 Universe
    Saga M:1 Universe
    

    I also agree with the other answer that there might be cases where a book could be in multiple universes, but it's up to you to decide if that's allowed or not :)