Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESTAdapter] Allow new parent, child to be saved at once #437

Closed
pivotal-medici opened this issue Oct 25, 2012 · 10 comments
Closed

[RESTAdapter] Allow new parent, child to be saved at once #437

pivotal-medici opened this issue Oct 25, 2012 · 10 comments
Labels
🏷️ feat This PR introduces a new feature

Comments

@pivotal-medici
Copy link

When the relationship branch was merged, it removed the concept of one model "waiting on" another model (via a "pending" state). This means it is no longer possible to commit a new parent and child at the same time, like so:

App.Person = DS.Model.extend({
  name: DS.attr('string'),
  parent: DS.belongsTo('App.Person'),
  children: DS.hasMany('App.Person')
});

parent = store.createRecord(App.Person, {
  name: 'Victor'
});

child = store.createRecord(App.Person, {
  name: 'Alex',
  parent: parent
});

store.commit();

At this point, there will be two people in the database, and (since we're assuming a relational database) we'd expect that the child would have a parent_id equal to parent.id. However, the child's parent_id is actually saved as NULL.

The actual commit of the new child record by the REST adapter needs to wait for the parent's commit to finish so that it can obtain and use the parent's ID.

@sly7-7
Copy link
Contributor

sly7-7 commented Oct 25, 2012

👍 I think there is a more general concept behind this. I would love @wycats, @tomdale or @tchak around this.
In fact, when having create/update/delete of records involved in relations, client and server are potentially not in synch after the different server response. For me it was already the case before the merge with the RI branch.

imagine you modify a parent attribute + add a child. So two requests are send to the server: one for the parent, one for the child. But the server process the parent and answer with no child, then the child (so server-side, the parent has now one child). As a result, after the didSaveRecord, client-side, the parent does not have a child :/

I hope I've been clear...

@tomdale
Copy link
Member

tomdale commented Oct 25, 2012

Because the notion of dependencies while saving is a fully adapter-level concern, we removed it from the store. Unfortunately, we have not yet had time to add the functionality back into DS.Adapter.

Our long-term plan is to add similar waitingOn semantics as a convenience on the adapter. Your adapter would internally do the book-keeping (by creating a graph of dependencies, let's say) and only acknowledge each record as completed as it was saved.

In the meantime, you are welcome to implement this in your adapter manually. You may want to look at our old implementation for inspiration.

Basically, you would look through each changed record with a changed belongsTo relationship and see if the other side had an ID. If it did, you would save immediately. Otherwise, you would create an observer on the other record's ID, and when it was assigned one, you would trigger the code that attempted to save the child record.

@ekosz
Copy link

ekosz commented Oct 26, 2012

+1 Just came across this issue now. This feature would be very helpful to have.

@elliterate
Copy link
Contributor

We've added an implementation to our pull request, #440.

@Energiz0r
Copy link

+1 Having the same issue here. Would really like this issue to be fixed.

@megpha
Copy link

megpha commented Dec 15, 2012

+1 Having the same issue.

@whather
Copy link

whather commented Dec 24, 2012

+1 Same. I assume you're working on a fix for this from the embedded-records branch? Any idea when this will be merged into master?

Thanks!

@thhermansen
Copy link

👍 Good work on both ember and ember data. Making this issue work will make it even better :)

@Energiz0r
Copy link

Any update on this?
The label of this issue states that it is an improvement but the documentation of Transaction states:

### Asynchronous Commits

Typically, all of the records in a transaction will be committed
together. However, new records that have a dependency on other new
records need to wait for their parent record to be saved and assigned an
ID. In that case, the child record will continue to live in the
transaction until its parent is saved, at which time the transaction will
attempt to commit again.

Should this be a bug, with a 1.0 milestone?

@stefanpenner
Copy link
Member

closing in favour of: #724

@runspired runspired added 🏷️ feat This PR introduces a new feature and removed Improvement labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ feat This PR introduces a new feature
Projects
None yet
Development

No branches or pull requests