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

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

Closed
demiro opened this issue Dec 4, 2019 · 4 comments
Closed

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

demiro opened this issue Dec 4, 2019 · 4 comments

Comments

@demiro
Copy link

demiro commented Dec 4, 2019

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

+-- loopback@3.26.0
+-- loopback-boot@2.28.0
+-- loopback-component-explorer@6.4.0
+-- loopback-connector-mongodb@5.0.1
+-- loopback-slug@0.2.3
@demiro demiro added the bug label Dec 4, 2019
@bajtos
Copy link
Member

bajtos commented Dec 5, 2019

@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

@yshing
Copy link
Member

yshing commented Dec 9, 2019

@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"
    }
}

@stale
Copy link

stale bot commented Feb 9, 2020

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 stale bot added the stale label Feb 9, 2020
@stale
Copy link

stale bot commented Feb 23, 2020

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 Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants