Nicolas Liveris
Nicolas Liveris

Reputation: 33

loopback hasandbelongstomany relation table doesn't exist

I have defined two models: a 'part' model:

{
  "name": "part",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "assemblies": {
      "type": "hasAndBelongsToMany",
      "model": "assembly",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

and an 'assembly' models:

{
  "name": "assembly",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "parts": {
      "type": "hasAndBelongsToMany",
      "model": "part"
    }
  },
  "acls": [],
  "methods": {}
}

both models have an hasAndBelongToMany relation.

In a /server/boot/sample-model.js i have created some instance of this both models:

module.exports =  function(app){
  var Dev = app.dataSources.dev;
  var Customer = app.models.customer;
  var Order = app.models.order;
  var Part = app.models.part;
  var Assembly = app.models.assembly;

  Dev.automigrate(['customer', 'order', 'assembly', 'part'], function(err) {
    Customer.create([
      {name: 'nicolas'},
      {name: 'marie'},
      {name: 'cyril'}
    ], function(err, customers){
      Part.create([
        {name: 'boulon'},
        {name: 'ecrou'},
        {name: 'cheville'},
      ], function(err, part){
        //console.log(part[0])
        Assembly.create([
          {title: 'piece1'},
          {title: 'piece2'},
          {title: 'piece3'},
        ], function(err, assemblies){
          //console.log(assemblies[0])
          assemblies[0].parts.add(part[0], function(err){
            if(err){
              console.log(err)
            }
          })
        })
      })
    });
  });
}

but

assemblies[0].parts.add(part[0], function(err){
                if(err){
                  console.log(err)
                }
              })

end with error:

{ [Error: ER_NO_SUCH_TABLE: Table 'database_development.assemblypart' doesn't exist]
  code: 'ER_NO_SUCH_TABLE',
  errno: 1146,
  sqlState: '42S02',
  index: 0 }

why loopback doesn't create the assemblypart table in my database ?

Upvotes: 2

Views: 1194

Answers (1)

LancelotP
LancelotP

Reputation: 319

I ran into this same issue once and after many hours of struggling (using postgres connector), I've found several solutions.

Here's the shortest :

Instead of :

  Dev.automigrate(['customer', 'order', 'assembly', 'part'], function(err) {
    // Your code here
  });

Try using this :

  Dev.automigrate()
    .then(function(err) {
      // Your code here
    });

I don't know exactly why, but in the second case the junction table is created.

Can you try this and let me know if it works for you ? If it's not can you provide some place where I can inspect and try running your code ?

Upvotes: 3

Related Questions