PB&J are a stand in for the real data I want to keep a secret. I want to catalog peanut butter & jelly sandwiches (PB&J) offered by different restaurants to determine which restaurants offer the sandwich which most matches a user's taste. The constraints:
- PB&J sandwich has components:
- BREAD (white, wheat, brioche, etc.).
- JELLY (strawberry, grape, etc.).
- PEANUT BUTTER (creamy, crunchy, etc.).
- OTHER INGREDIENTS (bananas, chia seeds, honey, marshmallow fluff, etc.).
- CUT (square, triangle, etc.).
- You may only select one of the following components per sandwich (BREAD, JELLY, PEANUT BUTTER, CUT).
- You may select one or more of the OTHER INGREDIENTS.
- A Restaurant may have one or more PB&J's assigned to it.
- A User will have one PB&J assigned to them (denoting their preference).
I do not like this approach because of the
pbj_sandwiches
table relying on matching id's with the tables they belong to in separate columns:
users
:
- id - int PK
- name - string
- email - string
restaurant
:
- id - int PK
- name - string
user_sandwiches
:
- id - int PK
- user_id - int FK UNIQUE > user.id
- pbj_sandwich_id - int FK >- pbj_sandwiches.id
restaurant_sandwiches
:
- id - int PK
- restaurant_id - int FK > restaurant.id
- pbj_sandwich_id - int FK >- pbj_sandwiches.id
pbj_sandwiches
:
- id
pbj_sandwich_components
:
- id
- pbj_sandwich_id - int FK >- pbj_sandwiches.id
- component_id int FK (pbj_sandwich_bread.id, pbj_sandwich_jellies.id, pbj_sandwich_peanut_butters.id, pbj_sandwich_other_ingredients.id, pbj_sandwich_cuts.id)
- component_table - string (pbj_sandwich_bread, pbj_sandwich_jellies, pbj_sandwich_peanut_butters, pbj_sandwich_other_ingredients, pbj_sandwich_cuts)
pbj_sandwich_bread
:
- id
- name
pbj_sandwich_jellies
:
- id
- name
pbj_sandwich_peanut_butters
:
- id
- name
pbj_sandwich_other_ingredients
:
- id
- name
pbj_sandwich_cuts
:
- id
- name
I could treat the components like a taxonomy maybe? Should the component types be stored more statically in an ENUM in code instead of database (but what about when a new option needs to be added)? I need to enforce a single selection of some components, but allow multiple selections of others. The schema provided I am not happy with.