emberfire icon indicating copy to clipboard operation
emberfire copied to clipboard

Partially fixed many-to-many relationship issue

Open charlesfries opened this issue 6 years ago • 10 comments

I think I have a fix for the many-to-many relationship issue people have been experiencing. Please don't merge this in though, as the code is pretty poor right now. To complete the code we just need a good way of determining if a given relationship's inverse attribute is a belongsTo or a hasMany. Currently I am doing that by checking if the last character is an 's' or not, which obviously is less than ideal. But it does seem to work.

TLDR: This code will work in a pinch if your models follow standard inflection (e.g. "users/user", but not "people/person").

charlesfries avatar Oct 25 '19 19:10 charlesfries

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar Oct 25 '19 19:10 googlebot

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar Oct 25 '19 19:10 googlebot

I fortunately have a better way to detect the inverse relationship.

What about saving the associated _ids?

jamesdaniels avatar Oct 25 '19 20:10 jamesdaniels

Is this what you mean by saving the associated IDs? The code below is what I'm using to save models with a many-to-many rel:

this.store.createRecord('club', { title: 'My Club' }).save().then(club => {
  let member = this.store.createRecord('member', { name: 'Bob' });
  member.clubs.pushObject(club);
  member.save().then(savedMember => {
    club.members.pushObject(savedMember);
    club.save();
  });
});

This requires saving the club model twice. I don't know 100% how Ember Data intended relationship fetching/saving to work, but are you implying that the associated IDs should be persisted automatically if a many-to-many relationship is detected?

charlesfries avatar Oct 25 '19 20:10 charlesfries

@googlebot I signed it!

charlesfries avatar Oct 25 '19 20:10 charlesfries

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar Oct 25 '19 20:10 googlebot

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar Oct 25 '19 20:10 googlebot

I think we can save the inverse record in a batch. I’ll look into this more, thanks for getting this kicked off.

jamesdaniels avatar Oct 26 '19 05:10 jamesdaniels

@jamesdaniels Any plans to continue this work please?

Alonski avatar Mar 15 '20 18:03 Alonski

@charlesfries is your solution tested enough that i can roll a custom fork and begin to leverage many to many relationships again?

mohitsud avatar Sep 13 '20 05:09 mohitsud