Loopback: LB3: multiple hasOne relations on a one model not working for me

Created on 4 Dec 2019  路  4Comments  路  Source: strongloop/loopback

Steps to reproduce

I have two simple models (using mongoDB for DB)...
I have a model called wedding and another one called person

on the wedding I have 3 relations

{
    "people": {
      "type": "hasMany",
      "model": "person",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    },
    "partner1": {
      "type": "hasOne",
      "model": "person",
      "foreignKey": ""
    },
    "partner2": {
      "type": "hasOne",
      "model": "person",
      "foreignKey": ""
    }
}

So my wedding document in DB looks something like this:

{
    "_id": "5de78c76f89d1a8ad4091ca5",
    "date": "2019-12-04T10:37:42.000Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "created": "2019-12-04T10:37:42.720Z",
    "partner1Id": "5de78c77f89d1a8ad4091ca6",
    "partner2Id": "5de78c77f89d1a8ad4091ca7"
}

Current Behavior

When I do filter:
{ "include": ["partner1", "partner2"]}
on
http://localhost:3000/api/weddings/5de78c76f89d1a8ad4091ca5

I am getting this:

{
  "date": "2019-12-04T10:37:42.000Z",
  "id": "5de78c76f89d1a8ad4091ca5",
  "userId": "5de78c76f89d1a8ad4091ca4",
  "created": "2019-12-04T10:37:42.720Z",
  "partner1Id": "5de78c77f89d1a8ad4091ca6",
  "partner2Id": "5de78c77f89d1a8ad4091ca7",
  "partner1": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  },
  "partner2": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  }
}

Expected Behavior

What I should have gotten is:

{
  "date": "2019-12-04T10:37:42.000Z",
  "id": "5de78c76f89d1a8ad4091ca5",
  "userId": "5de78c76f89d1a8ad4091ca4",
  "created": "2019-12-04T10:37:42.720Z",
  "partner1Id": "5de78c77f89d1a8ad4091ca6",
  "partner2Id": "5de78c77f89d1a8ad4091ca7",
  "partner1": {
    "id": "5de78c77f89d1a8ad4091ca6",
    "fullName": "Michael Knight",
    "spouse": "spouse1",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  },
  "partner2": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  }
}

Additional information

+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
bug help wanted juggler-relations stale

Most helpful comment

@demiro I think you setup the relation in a revert way.
What you want is actually belongs to (targetId stored in current Model)

https://loopback.io/doc/en/lb2/BelongsTo-relations.html

{
    "people": {
      "type": "hasMany",
      "model": "person",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    },
    "partner1": {
      "type": "belongsTo",
      "model": "person",
      "foreignKey": "partner1Id"
    },
    "partner2": {
      "type": "belongsTo",
      "model": "person",
      "foreignKey": "partner2Id"
    }
}

All 4 comments

@demiro thank you for reporting the bug. I don't see any obvious problems in your description, it may be a bug in the framework. Would you like to investigate it yourself? My recommendation is to start here: https://github.com/strongloop/loopback-datasource-juggler/blob/2f867576b66fe535aa1f82bceaaa680ecd4eec6c/lib/include.js#L889-L960

@demiro I think you setup the relation in a revert way.
What you want is actually belongs to (targetId stored in current Model)

https://loopback.io/doc/en/lb2/BelongsTo-relations.html

{
    "people": {
      "type": "hasMany",
      "model": "person",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    },
    "partner1": {
      "type": "belongsTo",
      "model": "person",
      "foreignKey": "partner1Id"
    },
    "partner2": {
      "type": "belongsTo",
      "model": "person",
      "foreignKey": "partner2Id"
    }
}

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.

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.

Was this page helpful?
0 / 5 - 0 ratings