JonathanPrince / loopback-component-fixtures

handle fixtures for testing clients against
MIT License
12 stars 13 forks source link

Support hasAndBelongsToMany models #18

Open bostondv opened 7 years ago

bostondv commented 7 years ago

Fixture setup fails when trying to populate the auto-generated intervening model between two models with hasAndBelongsToMany relation.

Based on this example using an AssemblyPart.json fixture will fail on setup.

{
  "error": {
    "name": "TypeError",
    "status": 500,
    "message": "Cannot read property 'create' of undefined",
    "stack": "TypeError: Cannot read property 'create' of undefined\n    at loadFixture (/path/server/node_modules/loopback-component-fixtures/index.js:26:24)\n    at /path/server/node_modules/loopback-component-fixtures/node_modules/async/lib/async.js:181:20\n    at Object.async.forEachOf.async.eachOf (/path/server/node_modules/loopback-component-fixtures/node_modules/async/lib/async.js:233:13)\n    at Object.async.forEach.async.each (/path/server/node_modules/loopback-component-fixtures/node_modules/async/lib/async.js:209:22)\n    at loadFixtures (/path/server/node_modules/loopback-component-fixtures/index.js:46:9)\n    at Function.Fixtures.setupFixtures.app.setupFixtures [as setupFixtures] (/path/server/node_modules/loopback-component-fixtures/index.js:92:5)\n    at SharedMethod.invoke (/path/server/node_modules/strong-remoting/lib/shared-method.js:263:25)\n    at HttpContext.invoke (/path/server/node_modules/strong-remoting/lib/http-context.js:389:12)\n    at phaseInvoke (/path/server/node_modules/strong-remoting/lib/remote-objects.js:654:9)\n    at runHandler (/path/server/node_modules/loopback-phase/lib/phase.js:135:5)\n    at iterate (/path/server/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n    at Object.async.eachSeries (/path/server/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9)\n    at runHandlers (/path/server/node_modules/loopback-phase/lib/phase.js:144:13)\n    at iterate (/path/server/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n    at /path/server/node_modules/loopback-phase/node_modules/async/lib/async.js:157:25\n    at /path/server/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25"
  }
}

AssemblyPart.json:

[
  {
    "assumblyId": 1,
    "partId": 1
  }
]

Is this a bug / missing feature or am I doing something wrong?

Thanks!

JonathanPrince commented 7 years ago

Hello @bostondv, thank you for opening this issue. My first guess is that the through table hasn't been generated at the point when the fixtures are loaded. I guess you are defining this relationship programmatically? If so, you could try defining the AssemblyPart model explicitly, this should fix this issue. Please let me know if this helps or not.

bostondv commented 7 years ago

Hey @JonathanPrince, that's correct. I was using hasAndBelongsToMany relations which automatically generates the through model. Eg:

"relations": {
    "parts": {
      "type": "hasAndBelongsToMany",
      "model": "Part",
    }
}

I tried your suggestion and explicitly defined the through model with a hasMany relation on the Assembly and Part models and the setup worked without any problems.

Do you think it'd be possible to support the generated relations in the future though?

JonathanPrince commented 7 years ago

Hi @bostondv, sorry it's taken a while to get back to you. Perhaps this could be added as an extra config option to check each model for any through tables that are not explicitly defined and create them as needed. It's not a priority for me at this stage, if you're keen PR's are always welcome.