how to model multiple associations to one table
See original GitHub issueIn my app plants are delivered to nurseries.
Deliveries can originate from nurseries or terminate there. Thus the deliveries table has two relations to the nurseries table with the corresponding fields being:
from_nursery_idto_nursery_id
I can create an association for one of these in the delivery table:
static associations = {
nursery: { type: 'belongs_to', key: 'from_nursery_id' }
}
But how can I build the second? An object cannot have two keys with the same name…
I had originally created them using other names:
static associations = {
from_nursery: { type: 'belongs_to', key: 'from_nursery_id' },
to_nursery: { type: 'belongs_to', key: 'to_nursery_id' }
}
Until watermelondb told me there were missing associations. Then I realized the key has to be the table name.
The same problem occurs at the other side of the relations, at the nursery table. There I originally had:
static associations = {
from_delivery: { type: 'has_many', foreignKey: 'from_nursery_id' },
to_delivery: { type: 'has_many', foreignKey: 'to_nursery_id' }
}
and it seems I can only add one of the two associations here too.
What will be the downsides of missing associations?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Multiple associations with one table - ruby on rails
I've got a Group model which has a manager_id, a designer_id and other user ids corresponding to different user roles in this group....
Read more >Creating multiple associations with the same table
Always make the association name unique (try and come up with a good name, it will really help the readability of your code),...
Read more >Ecto: multiple associations to one table? - Elixir Forum
I would use a “has many through” association. The association table would have an extra column for size (big, medium, small). Convenience ...
Read more >Options for structuring multiple associations to a record
So if there's only one owner for a product, then just put a foreign key value that links to each potential owner table...
Read more >How to model multiple associations between tables - 1194521
How do I model this. There are basically two associations between the same tables. Most suggestions I have read somehow summarize as joining ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@radex Is this still not possible? I’m trying to do something similar
As we are currently not able to execute joins (besides
experimentalJoinTablesand ), I believe the possibility to relate the same table multiple times is very important in many scenarios.In my case, I have a “trainer_trainees” table that has the columns “trainer_id” and “trainee_id”. Both columns point to User.
BTW, thank you for the great work, @radex . After trying to use RxDB, I’ve being enjoying very much WatermlonDB for its simplicity.