question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to model multiple associations to one table

See original GitHub issue

In 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_id
  • to_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:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
KrisLaucommented, Oct 13, 2021

@radex Is this still not possible? I’m trying to do something similar

0reactions
g-nogueiracommented, Jun 13, 2022

As we are currently not able to execute joins (besides experimentalJoinTables and ), 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found