Usecase with unsaved models #477

rogovdm opened this Issue Jul 7, 2014 · 3 comments

4 participants


Zoo has animals.

  1. I create a new animal. Does not save in DB. It is isNew()=true

  2. I make a fetch of zoo entity.

  3. Animal gets zoo id = null.

I believe it's because fetching zoo entity overrides animals array without unsaved animal that was created in step one.

I understand that I can avoid this problem not calling fetch before I save an animal. But in my case I need to constantly update zoo entity in background. So it not my solution.

Any ideas?


@rogovdm Yeah I think your account is correct. Except if the animal was new before the fetch, wouldn't its ID have been null/undefined before and after the fetch? Either way, after the fetch, the animal is no longer part of the zoo. I'm not too familiar with these stretches of the code, but I believe it happens somewhere in a method of HasMany.prototype, where animals keyContents array is checked and the zoo animals collection is rebuilt from that array.

I've had many struggles along these lines, and it was pretty frustrating before I gained an understanding of how this works internally. Basically whenever a model doesn't have an ID, using it with Backbone Relational leads to bad things.


@dminkovsky Do you mind sharing your solution to this problem?


Hi @ricardograca, sorry, I've been away from GitHub.

Umm, there is no simple answer to this issue. For me it's been a combination of

1) Using IDs whenever possible, and

2) Understanding the behavior of related models when there is no ID, which is laid out best in the tests. For example, here: There may be other spots in the tests where this behavior is also demonstrated. If you find them, please post here.

@bpatram bpatram added the bug label Mar 22, 2016
@bpatram bpatram closed this Mar 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment