Relations with multiple fields and references, or with constraints.
Lets say I have a table
food
and a table nutrient
.
The food table contains an id
and a name
.
The nutrient table contains an id
, a type
and a name
.
I want to create a many-to-many relation about the food to their nutrients, so I did a third table: food-nutrient
.
The food-nutrient table as a foodId
, a nutrientId
and for now an amount
.
This setup seems to be doable with the many-to-many example, I did it just fine...
I can now query food.foodNutrients
and food.foodNutrients[number].nutrient
.
NOW, lets say, I want food.macros
and food.micros
which are the same relations but with a new constraint on nutrient.type
.
Is it doable and how?
In MySQL, I would do this with a join (like the following) but I would rather use a relation if possible:
I saw that relation accept arrays in one
function config arg for fields
and references
, I am playing with it right now, but did not figure how to do it yet, I would like to be able to specify something like a where
.5 Replies
This is very similar to polymorphic associations but not quite
Do you mind sharing the schema as it is right now?
My schema look like this:
I used polymorphic before but I did not think of this as a polymorphic relation, as I am not filtering on the same table.
For me it was more like "relation meet filtering"
I actually have an open PR for a feature like this. It can be applied for Polymorphic assoc but it doesn't have to
You can add your like and comment to see if the team picks it up
GitHub
Feat: optional where clause in relations by Angelelz · Pull Request...
This PR will close #1437, and will close #1051, will address part of #674 and part of #821.
Upon successful merging of this PR, a new where option will be added to the relation configuration. The m...
This would address indeed some of the cases.
Nice to see I am not just asking some dumb questions, but asking about things that actually are looked upon.
In the mean time, I think joins will do the jobs...