node-persist icon indicating copy to clipboard operation
node-persist copied to clipboard

Using include on the same model

Open wbrady opened this issue 13 years ago • 2 comments

If you have say a User which has a property "manager" which references another User you can't get the manager back properly using .include(). When you do User.include("manager").getById(id) for example it will return the manager (not the original user whose id you passed into getById) and that manager will have the property "manager" set to itself

wbrady avatar Sep 13 '12 19:09 wbrady

This is probably related:

If I have a model with two properties that are hasMany's to a second model, including both properties screws things up (but only including one works fine). For example if I have a User model that has two hasMany properties "itemsBought" and "itemsLiked" that both reference Items, trying to do User.include(["itemsBought", "itemsLiked"].getById(id) will give you back "itemsLiked" correctly but "itemsBought" will be returned with a list of items that are actually in "itemsLiked".

wbrady avatar Sep 13 '12 21:09 wbrady

The issue is here https://github.com/nearinfinity/node-persist/blob/master/lib/sqltree.js#L146-L148

It checks if the column belongs to that model. But when you you're including something that's the same model both copies of the field will belong to the model and will therefore get set once and then overwritten the second pass.

wbrady avatar Jul 22 '13 19:07 wbrady