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

Create Model with relations overwrites foreignKey #502

Closed
1 of 2 tasks
primissus opened this issue Apr 2, 2019 · 3 comments
Closed
1 of 2 tasks

Create Model with relations overwrites foreignKey #502

primissus opened this issue Apr 2, 2019 · 3 comments

Comments

@primissus
Copy link

primissus commented Apr 2, 2019

Bug or feature request

  • Bug
  • Feature request

Steps to reproduce

Having two models:

ModelA

{
  "name": "ModelA",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

and ModelB

{
  "name": "ModelB",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "modelA": {
      "type": "belongsTo",
      "model": "ModelA",
      "foreignKey": "modelaId"
    }
  },
  "acls": [],
  "methods": {}
}

Notice that ModelBhas a belongsTo relation with ModelA.

  1. Create one ModelA
{
    "_id" : ObjectId("5ca3b7a61076971a4308c4ce")
}
  1. Create one ModelB just setting modelaId
{
  "modelaId": "5ca3b7a61076971a4308c4ce",
}

result will include ObjectId which I consider is correct:

{
    "_id" : ObjectId("5ca3b7f81076971a4308c4cf"),
    "modelaId" : ObjectId("5ca3b7a61076971a4308c4ce")
}
  1. Create another ModelB but this time including modelA related model.
{
    "modelaId": "5ca3b7a61076971a4308c4ce",
    "modelA": {
    	"id": "5ca3b7a61076971a4308c4ce"
	}
}

the result will have modelaId as string wich will have the query filter issue from #3593

Link to reproduction sandbox

https://github.com/primissus/loopback-relation-bug-sandbox/blob/master/README.md

Expected result

Both methods should result in a model including ObjectId properties.

Additional information

darwin x64 11.4.0

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

@primissus
Copy link
Author

While debugging I found that the change of value occurs at loopback-datasource-juggler/lib/model.js:226

I undestand that model's foreign key is obtained from relation id, but I think it should check if should be converted to ObjectId.

@stale
Copy link

stale bot commented Jun 18, 2019

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 Jun 18, 2019
@stale
Copy link

stale bot commented Jul 2, 2019

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 Jul 2, 2019
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

2 participants