Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasManyThrough on same model can be inconsistent #571

Closed
ningsuhen opened this issue Apr 24, 2015 · 1 comment
Closed

HasManyThrough on same model can be inconsistent #571

ningsuhen opened this issue Apr 24, 2015 · 1 comment

Comments

@ningsuhen
Copy link
Contributor

I have a HasManyThrough relation from user model to user model itself via a through model "Follower".

"relations": {
    "follower": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "followerId"
    },
    "followee": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "userId"
    }
  },

On calling someUser.followers.add(anotherUser), the behavior is inconsistent. Depending upon the order of relations defined in Follower, it creates a new Follower object but with wrong values of followerId & userId.
E.g. after calling someUser.followers.add(anotherUser), the correct Follower Object should be

{
followerId:[anotherUser's id]
userId:[someUser's id]
}

However, it can be filled incorrectly like this:

{
followerId:[someUser's id]
userId:[anotherUser's id]
}

This is because of :
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/relation-definition.js#L868
findBelongsTo returns an array of foreign keys with no specific order but as defined in the model json. But the result is consumed with the expectation that first value will be the foreign key for base model.

P.S. I have a fix for this. Will send pull request soon. this is an issue for tracking purposes in case other users find the same problem.

ningsuhen added a commit to walkonsocial/loopback-datasource-juggler that referenced this issue Apr 24, 2015
raymondfeng pushed a commit that referenced this issue Apr 25, 2015
 * Fix the test cases (Raymond Feng)

 * Add support for merging include filters (ningsuhen)

 * add test case for hasmanythrough bi-drectional relations (ningsuhen)

 * Fix for bug - #571 (ningsuhen)
@ningsuhen
Copy link
Contributor Author

Fixed by ef0a10d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant