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

Use Include filter to get polymorphic belongsTo relation #1319

Closed
zane-insta opened this issue Apr 20, 2015 · 14 comments
Closed

Use Include filter to get polymorphic belongsTo relation #1319

zane-insta opened this issue Apr 20, 2015 · 14 comments
Assignees
Labels

Comments

@zane-insta
Copy link

Hi,

I have a polymorphic comment model that can belong to several other models. I want to be able to query the comment and include the parent model that it belongs to.
I have my comment model defined below.

{
  "name": "comment",
  "validations": [],
  "relations": {
    "commentable": {
      "type": "belongsTo",
      "polymorphic": true
    },
...

I am trying to query it by calling ...

var filter = {
   include: {
      relation: 'commentable'
   }  
}; 

Comment.findById({id: notification.parentId, filter: filter})
.$promise
.then(function(res) {
   console.log('Result: ' + res);
});

I receive the comment back fine but the commentable type is not retrieved properly.
I dug into the debug logs and I believe there is a bug somewhere because when it queries the database for the related model it says

all +0ms myModel { where: { id: 1}, limit: 1, offset: 0, skip: 0 }

but it should be asking for

all +0ms myModel { where: { myId: 1}, limit: 1, offset: 0, skip: 0 }

since I have a custom id member named myId instead of the default id.

@raymondfeng
Copy link
Member

@zane-insta Can you provide a link to your github repo that contains the test case?

@zane-insta
Copy link
Author

@raymondfeng Here is an example repository outlining the issue.
https://github.com/zane-insta/polyInclude

I have three tests that can be run with mocha test.
The first testcase shows successful retrieval of the hasMany polymorphic relations. The second shows the successful retrieval of the belongsTo polymorphic relation when the model does not have a custom id and the third testcase shows the failing scenario where the belongsTo model has a custom id.

@SumeetR
Copy link

SumeetR commented Mar 30, 2016

Hello,

Any update on this issue? I am experiencing the same as @zane-insta as I have a Catalogue model that represents various types of content, having a 'belongsTo' relationship with them, and really need to be able to include the original content with the Catalogue model.

Cheers,
S.

@loay
Copy link
Contributor

loay commented Apr 7, 2016

Hi @zane-insta , that's what I get if I run your tests:


  Top
    Get myModel with its comments
      √ Should get a myModel and all of its comments
    Get comment including otherModel
      √ Should get a comment and otherModel
    Get comment including myModel
[ { id: 1, commentableId: 1, commentableType: 'myModel' } ]
null
      1) Should get a comment and myModel


  2 passing (31ms)
  1 failing

I believe line 16 should be myId:1 and not id:1 .. then the error message will be:

[ { commentableId: 1, commentableType: 'myModel', myId: 1, id: 1 } ]
null
      1) Should get a comment and myModel

BTW, if we set id=2 in line 88, the test will pass.

if there is something that I am missing, please clarify further. thanks.

@zanemcca
Copy link

zanemcca commented Apr 8, 2016

Sorry I do not use @zane-insta anymore.
Line 16 is just there to give the first comment id: 1 for convenience. Same with line 25.

So changing line 88 would turn the second and third test into the same test which defeats the purpose.

In test 2 you can see that otherModel is successfully included and it does not have a custom id.
In test 3 you can see that myModel is not included and it does have a custom id. This is the test that outlines the error. A related model should always be included even if the id is customized.

@loay
Copy link
Contributor

loay commented Apr 13, 2016

@zanemcca
Sorry the tag mention goes directly to the name you used to open the issue.
I am having another look. thanks for clarifying.

@sprypradeep
Copy link

We are facing similar issues, is the polymorphic relation unusable then ? No update on such a critical issue.

@qaiser110
Copy link

I have the same issue. Any update on this?

@javid-netlinks
Copy link

Facing the same issue, any updates???

@raghavadss
Copy link

I'm facing the same issue. With a custom Id, Polymorphic belongsTo relation is not fetching the object when using include filter! 👎

@stale stale bot added the stale label Sep 19, 2017
@stale
Copy link

stale bot commented Sep 19, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale
Copy link

stale bot commented Oct 3, 2017

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Oct 3, 2017
@0candy 0candy removed the triaging label Oct 3, 2017
@carson-nr
Copy link

for anyone searching this issue, you're best bet is probably a scope defined in /your-model.js

I"ve realized that fighting to separate the values in the json file, and the logic in the .js file was getting me nowhere

mainly because of this bug

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

No branches or pull requests

13 participants