I have a user who may have 0 or more foo's and bar's. Here is a diagram:
Each foo has a unique integer ord
.
If a user has a bar, then this implies that he has a foo with ord=1
. A user may not have foo with ord=1
without a bar.
My thought how to avoid logical inconsistencies is to simple never insert any foo_user
relationship with ord=1
. However when I want to query the foo of a user with smallest ord
I need to do this a bit complicated like this:
- Query: Check if bar relationship exists. If it does, get foo with
ord=1
- Query: Check if bar relationship exists. If it does, get foo with
- Query: If no bar bar relationship exists, get foo with smallest
ord
column.
- Query: If no bar bar relationship exists, get foo with smallest
Is there maybe a more convenient database structure for this scenario?
ord=1
. Its possible that a user has 10 bars but only 3 foos.