pouchdb-community / ember-pouch

PouchDB/CouchDB adapter for Ember Data
Apache License 2.0
281 stars 76 forks source link

hasMany relationship not updating from the belongsTo side. #116

Closed dweremeichik closed 8 years ago

dweremeichik commented 8 years ago

It appears that the hasMany relationships are not updating when a belongsTo record is created. The two models i am using are:

export default Model.extend({
  name: DS.attr('string', {defaultValue: ""}),
  valueCents: DS.attr('number', {defaultValue: 0}),
  type: DS.attr('string', {defaultValue: ""}),
  discounts: DS.hasMany('discount')
});

and

export default Model.extend({
  triggerQuantity: DS.attr('number'),
  percentage: DS.attr('number'),
  item: DS.belongsTo('item')
});

I am attempting to add a discount to an existing item with the following code:

addDiscount(item){
      let discount = item.store.createRecord('discount',{
        item: item,
        triggerQuantity: 3,
        percentage: 3
      });
      discount.save();
    }

According to the ember docs, this should "just work". In the database, I can see that discount record has the id of the item that it was attached to. However the item record just contains an empty array, instead of an array with a reference to the discount record.

I am using: Ember 2.4.1 ember-data 2.4.0 ember-pouch 3.1.1

backspace commented 8 years ago

You have to save the parent record as well. This isn’t typical for Ember Data, but it’s required in this case to save both ends of the relationship.

The documentation should be updated to reflect this. But people expecting behaviour like with other Ember Data adapters will not expect it.

(For background on this, see #16)

dweremeichik commented 8 years ago

Thanks for pointing me towards that. However it seems that even if I do that, it still leaves the array empty on the item side. I tried an approach similar to what you did in your initializer (mine in a component).

https://github.com/backspace/ember-relational-pouch-example/blob/master/app/initializers/data-populator.js

backspace commented 8 years ago

Are you saving the item after having saved the discount?

That code you linked to is pretty old, but I have this more recent acceptance test that constructs related objects.

With the code you have in your first comment, you could do something like this at the end of addDiscount: discount.save().then(() => item.save());

dweremeichik commented 8 years ago

Sorry for the lack of code. My computer is running offline for the moment. I did exactly as you mentioned and it is still not working. I'm beginning to wonder if there is an issue with my models or the way i set up ember pouch. The strange part is that the discount is getting the item id.

dweremeichik commented 8 years ago

Because this should be working, i will start a project from scratch tomorrow to test the functionality out and make sure it is isolated to this... I'll use the same ~version that you used in your adventure project, and rip off your ember pouch configs to make sure i didn't do something dumb.

dweremeichik commented 8 years ago

Okay, so I started from scratch, not using the same version as you, however 2.4 is supposed to be minor fixes and backwards compatible. Our adapters are almost identical (I added debugging to mine), and our config/environment files are similar in regards to ember-pouch. It appears that it is still not working, so this disproves my configs being bad theory. As you can see here, my approach is pretty spot on to what you mentioned earlier. As before, the discount is getting the ID, but no matter how I save it (order wise), the Item won't update it's reference to the discount.

dweremeichik commented 8 years ago

Just an update, I swapped ember-pouch out with emberfire (firebase) and the way I'm writing it seems to work there. I tried troubleshooting ember-pouch earlier, but my knowledge of both emberjs and ember-pouch are limited. I would like to investigate further, but it may be a while if at all before I get back to it. Unfortunately at the moment I need to get a prototype out.

backspace commented 8 years ago

I figured out what’s missing! Your application doesn’t implement the ember-pouch serialiser. If you add this file at app/serializers/application.js, the parent contains references to the IDs of its children:

import { Serializer } from 'ember-pouch';

export default Serializer.extend();

I’ll look at adding this to the documentation and/or addon default imports.

dweremeichik commented 8 years ago

Thank you, that did it! I owe you a beer or something!

I'm looking into adding to the documentation regarding saving both the child and parent for relationships, not sure where the best place for it would be though. I'm thinking probably before the "EmberPouch Blueprints" section. I will mull it over tonight, and create a pull request in the morning. I might open an issue for discussion on the documentation regarding organization and ordering.

dweremeichik commented 8 years ago

Issue solved, and documentation created on its behalf #119. Blueprint in progress #124.