sqlitegodatabase-design

How do I simplify this model with a double reference?


My CRUD application's data models:

enter image description here

What is the proper way to set up the FilmGroup model? It has a one-to-many relationship with Film and a one-to-one relationship with FilmOrder.

I need both FilmOrderID and FilmOrder to associate the FilmOrderID with this model, but also want easy access to the associated FilmOrder since I render all data to the user.

How do I simplify the FilmGroup model so it does not need FilmOrderID and a reference to a FilmOrder object? How to map FilmOrder *FilmOrder to a database field? I want to use the same struct for the database model and to display the object.


Solution

  • ...the FilmGroup model. It has a one to many relationship with Film, and a one to one relationship with FilmOrder.

    One-to-one relationships are suspect and difficult to enforce.

    Here's a simpler model assuming a Film can only be in one FilmGroup, and a FilmOrder can only be for one FilmGroup. Film and FilmOrder both reference FilmGroup.

    Film -> FilmGroup <- FilmOrder
    

    If a Film can be in more than one FilmGroup, you'd put the FilmGroup_Film join table back, but FilmOrder would still refer to FilmGroup.

    Film <- FilmGroup_Film -> FilmGroup <- FilmOrder
    

    Both of these have the possibility that a FilmGroup could be the subject of multiple FilmOrders. If you want to make it one-to-one, make FilmOrders.film_group_id unique.

    Notes