platanus / angular-restmod

Rails inspired REST-API ORM for Angular
http://platanus.github.io/angular-restmod/
MIT License
1.18k stars 87 forks source link

Disappearing relationship key fields #138

Open scriby opened 10 years ago

scriby commented 10 years ago

I have an object setup with a relationship, let's call it x. The key for the field is xId. I'm noticing that when creating the object, the xId field is not present, but I can read the id from x.id.

I have a case on my hands now where that behavior is not really desired. If I start with just the xId field, then later change it to be a relationship, all the code I've written against the xId field breaks. I am working on porting some $resource models over to restmod and encountering this.

I wasn't really sure what the reasoning was for removing the original key properties.

iobaixas commented 10 years ago

Well, the idea is to avoid data duplication and prevent handling id's directly. Besides, it would be complex to keep the relation object and id property synchronised.

I seems that a patch for this would require to redefine the relations attrAsReference and attrAsReferenceToMany builder methods, you can take a look at src/module/extended/builder-relations.js for more information about how do they work.

scriby commented 10 years ago

Ok, that's fine. This probably just deserves some mention in the docs so users know to expect the behavior.

scriby commented 10 years ago

Am I right that I can't set the relationshipId once I turn it into a relationship? So if I want to create an object and associate it with an existing object on the server, I can't just set relationshipId, but I need to create a $new instance of that type?

scriby commented 10 years ago

It seems like I can't do Model.$build({ relationshipId: 1 }) either to setup the relationship...

scriby commented 10 years ago

Looks like Model.$buildRaw({ relationshipId: 1 }) does work...

scriby commented 9 years ago

I just got hit by the "disappearing" relationship key field again when converting existing code to a relationship. I'd much prefer if we just left the key in tact, even if it didn't update automatically when the relationship changed later.

iobaixas commented 9 years ago

I'll add this to the docs