Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Keeping foreign keys when augmenting the model #464

sonicd300 opened this Issue · 12 comments

4 participants



I'd like to know the best way to get the related model but keep the foreign key as it was

relations: [{
    type: 'HasOne',
    key: 'owner',
    keySource: 'owner_id',
    keyDestination: 'owner',
    relatedModel: UserModel,
    collectionType: UsersCollection

My original key is owner_id, but im setting the related model into a different key augmenting my model, but i'd like to keep the foreign key to filter the collection by foreign keys

for example

this.collection.where({ owner_id: xyz })

If relational:
a) replaces my foreign key owner_id for the related model
b) append the related model to a different key in the json but removes the foreign key
the above code wont work.

So im trying to keep the foreign key, or have a nice way to filter the collection as mentioned above.


What i did locally was comment out these lines:

// Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'.
// if ( this.keySource !== this.key ) {
//  delete this.instance.attributes[ this.keySource ];
// }

But i think this will cause other bugs, what i'd like to do is to have a keepKey param or something that prevents this code to be excecuted and also update other places that may break without this.


The main problem with continuing to use keySource after the model that should be used for that relation has been resolved is that it won't keep up with your changes. So say that you change the owner to file.set( 'owner', Pete ), it will still point to whatever was the original owner when you loaded the data.


Ok, that's a valid point it will keep pointing to the original value, lets say without my change, if you do file.set( 'owner', Pete ) will that send a put request to the original model to update the reference?


If you perform or 'owner', Pete ) then yes, it will. But not before that.


Well i guess then, im going to add an update on the key when you perform a set and create a PR in the library :P



this.collection.where({ owner_id: xyz })

Wouldn't the more desirable way—in terms of the APIs that Backbone and Backbone Relational enable and provide for us—be to perform this search like:

var owner = UserModel.findOrCreate({ owner_id });
this.collection.where({ owner: owner });

What do you think? This should be fetch-state independent.


@dminkovsky: Thanks for your comment. I just tried your suggestion and works fine, i still think that keeping the foreign key is a good idea tough. I'll use this approach and if this works then i'll close the thread.


You're welcome @sonicd300, I hope it helps with your problem. It seems doing it this way is "nicer" and "truer" to the APIs.

I think BR keeps the foreign key, just where that foreign key is located depends on the fetch state. You can check out my recent issue on this topic #463.


I really need to keep foreign key in a model.

I work with owner model on my client, but have to send owner_id is some cases in my requests to the server. And Overridng Model's sync method happens to be over complicated.


@rogovdm There's also toJSON(), which is where keyDestination gets swapped in for key if present in the relation definition. Maybe wrap that function? I've been having similar issues as well—wanting to sent situation-dependent representations of a model to the server. Have not figured out a good way to make that happen.


@dminkovsky, thanks a lot. Good idea. Helped in my case.


@rogovdm glad it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.