loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Use Include filter to get polymorphic belongsTo relation

Open zane-insta opened this issue 11 years ago • 13 comments

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.

zane-insta avatar Apr 20 '15 20:04 zane-insta

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

raymondfeng avatar Apr 20 '15 20:04 raymondfeng

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

zane-insta avatar Apr 20 '15 23:04 zane-insta

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.

SumeetR avatar Mar 30 '16 10:03 SumeetR

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.

loay avatar Apr 07 '16 19:04 loay

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.

zanemcca avatar Apr 08 '16 00:04 zanemcca

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

loay avatar Apr 13 '16 18:04 loay

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

sprypradeep avatar Oct 15 '16 07:10 sprypradeep

I have the same issue. Any update on this?

qaiser110 avatar Feb 05 '17 00:02 qaiser110

Facing the same issue, any updates???

javid-netlinks avatar Apr 01 '17 04:04 javid-netlinks

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

raghavadss avatar Jul 21 '17 11:07 raghavadss

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[bot] avatar Sep 19 '17 11:09 stale[bot]

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[bot] avatar Oct 03 '17 11:10 stale[bot]

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

carson-nr avatar Oct 31 '19 16:10 carson-nr