3
votes

I am trying to manage ember-data transactions (creating and committing records) on a model that has two belongsTo relationships. In initially I was simply adding the new record to the transactions using (in Coffeescript): @transaction.createRecord(App.Book, {author: App.router.authorController.get('content')})

This sets the author attribute (which is a DS.belongsTo relationship) to current author and then in a form the user sets the other attributes for the book, including another belongsTo relationship from a select element (let's say the publisher). This works fine, but if the user goes to create a second new record I get the following assertion raised:

Ember.assert("All records in a changed relationship must be in the same transaction. You tried to change the relationship between records when one is in " + t + " and the other is in " + prev, t === prev);

This is from line 203 of the one_to_many_change.js file in Ember Data

I take this to mean that I must manually add all the relevant model objects into the transaction. So I could add the author model to the transaction using something like:

author = App.router.authorController.get('content')
@transaction.add(author)

And then do the same for any publisher models that might be affected involved in the transaction (I was doing these operations within the console). However, when I do this I then get the following error:

Error: assertion failed: Once a record has changed, you cannot move it into a different transaction

This occurs even though the previous (original) transaction had been committed. Just wondering how others are dealing with this issues. I imagine assigning this kinds of relationships within transactions must be common process in Ember Data apps.

1
I think your assertion here: "This occurs even though the previous (original) transaction had been committed" is either related to github.com/emberjs/data/pull/412 either because the server response is not yet arrived when you want to add the author. Could you apply the quick-fix I suggested in my PR, and tell me if it works ?sly7_7
@sly7_7 Thanks! That pull request seems to solve my issue... hopefully it will get merged soon. I'll add a comment on the pull request.Sean O'Hara
I don't know if this PR is the right one, it seems too simple. But anyway it's a good thing to have updated it with a link to here :)sly7_7
What I've been doing to solve this is to call .removeCleanRecords() on a transaction after committing it. I think this won't be an issue when we are able to move dirty records to different transactions.mehulkar

1 Answers

0
votes

It looks like your author object was modified and attached to the default transaction. Then you create a new transaction and create a book on this one.

You have then 2 records that need to be persisted, both of them are linked through an association but they are on a different transaction.

One way of doing it would be to use the author transaction to create your book

var book = author.get('transaction').createRecord(App.Book, {author: author});