loopback icon indicating copy to clipboard operation
loopback copied to clipboard

"embedsMany" cause problem with related model.

Open veekungx opened this issue 12 years ago • 10 comments

It ever worked when I used loopback below 2.8.9. Is this feature gone in 2.9.1?

"addresses" : {
          "type" : "embedsMany",
          "model" : "Address",
          "options": {
              "validate": true,
              "autoId": true
          }
      }

veekungx avatar Sep 11 '14 08:09 veekungx

Hi @veekungx - this feature is still available. To enable it, your Address id property needs to be of type Number. If you want to prevent someone to set this auto-id value manually, set forceId: true. I believe validate: true has become the default now.

Is your Address tied to a persistent dataSource? If not persisted separately, I suggest using a Transient dataSource instead. In that case, should you define the id property as a string, it will get a MongoDB-like random hex string value. But as Number it will work as before (an auto-incrementing sequence).

Note that there are a number of PR waiting to be merged, related to this:

https://github.com/strongloop/loopback-datasource-juggler/pull/290 https://github.com/strongloop/loopback-datasource-juggler/pull/295

@raymondfeng could you please review and merge? I worked on these after a similar question was raised.

fabien avatar Sep 11 '14 08:09 fabien

When I embedsMany to target model (ex. "Address"). The targeted model disable the auto id injection on POST /Addresses and POST /Users/{pk}/Addresses

Why this happen? LB v2.9.1

Address and User models bind to MongoDB.

veekungx avatar Sep 11 '14 09:09 veekungx

If Address is meant to be stored in MongoDB as well, you should definitely watch https://github.com/strongloop/loopback-datasource-juggler/pull/295 - and this commit specifically:

https://github.com/fabien/loopback-datasource-juggler/commit/95764232b9e69da31ca71145013df883df1390b0

Then, with {persistent: true} you'll get a regular MongoDB ObjectID value. And POST will work as expected. Please see the tests I added for that commit. I tested this with MongoDB too.

fabien avatar Sep 11 '14 10:09 fabien

@veekungx did you manage to resolve the issue?

fabien avatar Sep 11 '14 12:09 fabien

I will explain about these.

I generated two models "Parent" and "Address" with slc loopback:model.

I called POST /Addresses It auto generated "id" field and response.

{
  "id": "5411a040f66a5ac41607c80d"
}

Then I configured relation on "parent.json" like this.

"relations": {
"addresses" : {
          "type" : "embedsMany",
          "model" : "Address"
      }
}

I called POST /Addresses again (NOT POST /Parents/{pk}/Address) but this time it response with validation error "422"

{
  "error": {
    "name": "ValidationError",
    "status": 422,
    "message": "The `Address` instance is not valid. Details: `id` can't be blank.",
    "statusCode": 422,
    "details": {
      "context": "Address",
      "codes": {
        "id": [
          "presence"
        ]
      },
      "messages": {
        "id": [
          "can't be blank"
        ]
      }
    },
    "stack": "ValidationError: The `Address` instance is not valid. Details: `id` can't be blank.\n    at c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\dao.js:153:16\n    at ModelConstructor.<anonymous> (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\validations.js:462:11)\n    at ModelConstructor.next (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\hooks.js:66:12)\n    at ModelConstructor.<anonymous> (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\validations.js:459:23)\n    at ModelConstructor.trigger (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\hooks.js:56:12)\n    at ModelConstructor.Validatable.isValid (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\validations.js:435:8)\n    at Function.DataAccessObject.create (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback-datasource-juggler\\lib\\dao.js:149:7)\n    at SharedMethod.invoke (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback\\node_modules\\strong-remoting\\lib\\shared-method.js:207:17)\n    at HttpContext.invoke (c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback\\node_modules\\strong-remoting\\lib\\http-context.js:242:12)\n    at c:\\Users\\Ti\\DenlaAPI\\node_modules\\loopback\\node_modules\\strong-remoting\\lib\\remote-objects.js:475:9"
  }
}

Is it bug? I never found this on earlier version of LB

veekungx avatar Sep 11 '14 13:09 veekungx

Yes, it's a bug - the PR I mentioned fixes this - you will need the persistent: true option introduced as part of it.

fabien avatar Sep 11 '14 13:09 fabien

Thank you.

veekungx avatar Sep 11 '14 14:09 veekungx

I have same problem:

Ref to question: https://github.com/strongloop/loopback-datasource-juggler/issues/336#issuecomment-76734453

brunocascio avatar Mar 04 '15 13:03 brunocascio

Yes it is still a bug.

mailuser41 avatar May 25 '15 21:05 mailuser41

@veekungx Can this be re-opened? I am still seeing this issue.

james-gardner avatar Jul 28 '15 22:07 james-gardner