Keeping foreign keys when augmenting the model #464

Closed
sonicd300 opened this Issue May 21, 2014 · 12 comments

Comments

Projects
None yet
5 participants
@sonicd300

Hi,

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.

@sonicd300

This comment has been minimized.

Show comment Hide comment
@sonicd300

sonicd300 May 21, 2014

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.

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.

@PaulUithol

This comment has been minimized.

Show comment Hide comment
@PaulUithol

PaulUithol May 21, 2014

Owner

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.

Owner

PaulUithol commented May 21, 2014

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.

@sonicd300

This comment has been minimized.

Show comment Hide comment
@sonicd300

sonicd300 May 21, 2014

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?

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?

@PaulUithol

This comment has been minimized.

Show comment Hide comment
@PaulUithol

PaulUithol May 21, 2014

Owner

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

Owner

PaulUithol commented May 21, 2014

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

@sonicd300

This comment has been minimized.

Show comment Hide comment
@sonicd300

sonicd300 May 21, 2014

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

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

@dminkovsky

This comment has been minimized.

Show comment Hide comment
@dminkovsky

dminkovsky May 23, 2014

Regarding:

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.

Regarding:

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.

@sonicd300

This comment has been minimized.

Show comment Hide comment
@sonicd300

sonicd300 May 23, 2014

@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.

@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.

@dminkovsky

This comment has been minimized.

Show comment Hide comment
@dminkovsky

dminkovsky May 23, 2014

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.

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.

@rogovdm

This comment has been minimized.

Show comment Hide comment
@rogovdm

rogovdm Jul 2, 2014

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 commented Jul 2, 2014

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.

@dminkovsky

This comment has been minimized.

Show comment Hide comment
@dminkovsky

dminkovsky Jul 2, 2014

@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.

@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.

@rogovdm

This comment has been minimized.

Show comment Hide comment
@rogovdm

rogovdm Jul 2, 2014

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

rogovdm commented Jul 2, 2014

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

@dminkovsky

This comment has been minimized.

Show comment Hide comment
@dminkovsky

dminkovsky Jul 3, 2014

@rogovdm glad it helped!

@rogovdm glad it helped!

@bpatram bpatram added the question label Mar 26, 2016

@bpatram bpatram closed this Mar 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment