Many-to-many association #37

Closed
opened 2017-02-04 00:53:47 +00:00 by ghost · 4 comments
ghost commented 2017-02-04 00:53:47 +00:00 (Migrated from github.com)

Hello, I have these two associations defined

Team.belongsToMany(Project, {through: 'TeamProjects})
Project.belongsToMany(Team, {through: 'TeamProjects})

This is the error that I got. It looks like routes with the same path are added more than once and is rejected by Hapi. I'm not sure if I'm doing something wrong. Please help. Thanks

Error: New route /teams conflicts with existing /teams
    at Object.exports.assert (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/hoek/lib/index.js:736:11)
    at internals.Segment.add (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/call/lib/segment.js:51:14)
    at internals.Router.add (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/call/lib/index.js:62:26)
    at internals.Connection._addRoute (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/connection.js:416:37)
    at internals.Connection._route (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/connection.js:403:18)
    at internals.Plugin._apply (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/plugin.js:588:14)
    at internals.Plugin.route (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/plugin.js:558:10)
    at Object.exports.list (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:220:10)
    at Object.keys.forEach.method (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:81:20)
    at Array.forEach (native)
    at createAll (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:80:24)
    at exports.default (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:181:5)
    at Object.keys.forEach.modelName (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/index.js:117:24)
    at Array.forEach (native)
    at Object.register (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/index.js:115:23)
    at Object.target [as register] (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/joi/lib/object.js:77:34)```
Hello, I have these two associations defined `Team.belongsToMany(Project, {through: 'TeamProjects})` `Project.belongsToMany(Team, {through: 'TeamProjects})` This is the error that I got. It looks like routes with the same path are added more than once and is rejected by Hapi. I'm not sure if I'm doing something wrong. Please help. Thanks ``` Error: New route /teams conflicts with existing /teams at Object.exports.assert (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/hoek/lib/index.js:736:11) at internals.Segment.add (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/call/lib/segment.js:51:14) at internals.Router.add (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/call/lib/index.js:62:26) at internals.Connection._addRoute (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/connection.js:416:37) at internals.Connection._route (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/connection.js:403:18) at internals.Plugin._apply (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/plugin.js:588:14) at internals.Plugin.route (/Users/dlo/hapi-sequelize-test/node_modules/hapi/lib/plugin.js:558:10) at Object.exports.list (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:220:10) at Object.keys.forEach.method (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:81:20) at Array.forEach (native) at createAll (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:80:24) at exports.default (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/crud.js:181:5) at Object.keys.forEach.modelName (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/index.js:117:24) at Array.forEach (native) at Object.register (/Users/dlo/hapi-sequelize-test/node_modules/hapi-sequelize-crud/build/index.js:115:23) at Object.target [as register] (/Users/dlo/hapi-sequelize-test/node_modules/hapi/node_modules/joi/lib/object.js:77:34)```
mdibaiee commented 2017-02-04 06:42:24 +00:00 (Migrated from github.com)

Could you please post your entire models definitions and plugin registration code?

Could you please post your entire models definitions and plugin registration code?
ghost commented 2017-02-04 17:50:10 +00:00 (Migrated from github.com)

Sure, here they are.

team.js

module.exports = (sequelize, DataTypes) => {
  return sequelize.define('Team', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: DataTypes.STRING,
  }, {
    classMethods: {
      associate: function(models) {
        models.Team.belongsToMany(models.Player, {
          through: 'TeamPlayers'
        });
      },
    },
  });
};

player.js

module.exports = (sequelize, DataTypes) => {
  return sequelize.define('Player', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: DataTypes.STRING,
    active: DataTypes.BOOLEAN,
  }, {
    classMethods: {
      associate: function(models) {
        models.Player.belongsToMany(models.Team, {
          through: 'TeamPlayers'
        });
      }
    }
  })
};

index.js

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ port: 3000 });

const Sequelize = require('sequelize');

const sequelize = new Sequelize('mysql://root@127.0.0.1:3306/nodetest', {logging: false});

server.register({
  register: require('hapi-sequelize'),
  options: {
    name: 'test',
    models: ['./test/**/*.js'],
    sequelize: sequelize,
    sync: true,
    forceSync: true
  }
}, err => {

  if (err) {
    console.log(err);
  }
  
  server.register({
    register: require('hapi-sequelize-crud'),
    options: {
      name: 'test',
    },
  }, err => {

    if (err) {
      console.log(err);
    }

    server.start((err) => {

        if (err) {
            throw err;
        }

        console.log('server running at: ' + server.info.uri);
    });
  });
});

package.json

{
  "name": "hapi-sequelize-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "dependencies": {
    "hapi": "^16.1.0",
    "hapi-sequelize": "^3.0.4",
    "hapi-sequelize-crud": "^2.9.0",
    "mysql": "^2.13.0",
    "qs": "^6.3.0",
    "sequelize": "^3.24.6"
  }
}
Sure, here they are. team.js ``` module.exports = (sequelize, DataTypes) => { return sequelize.define('Team', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, }, name: DataTypes.STRING, }, { classMethods: { associate: function(models) { models.Team.belongsToMany(models.Player, { through: 'TeamPlayers' }); }, }, }); }; ``` player.js ``` module.exports = (sequelize, DataTypes) => { return sequelize.define('Player', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, }, name: DataTypes.STRING, active: DataTypes.BOOLEAN, }, { classMethods: { associate: function(models) { models.Player.belongsToMany(models.Team, { through: 'TeamPlayers' }); } } }) }; ``` index.js ``` const Hapi = require('hapi'); const server = new Hapi.Server(); server.connection({ port: 3000 }); const Sequelize = require('sequelize'); const sequelize = new Sequelize('mysql://root@127.0.0.1:3306/nodetest', {logging: false}); server.register({ register: require('hapi-sequelize'), options: { name: 'test', models: ['./test/**/*.js'], sequelize: sequelize, sync: true, forceSync: true } }, err => { if (err) { console.log(err); } server.register({ register: require('hapi-sequelize-crud'), options: { name: 'test', }, }, err => { if (err) { console.log(err); } server.start((err) => { if (err) { throw err; } console.log('server running at: ' + server.info.uri); }); }); }); ``` package.json ``` { "name": "hapi-sequelize-test", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "dependencies": { "hapi": "^16.1.0", "hapi-sequelize": "^3.0.4", "hapi-sequelize-crud": "^2.9.0", "mysql": "^2.13.0", "qs": "^6.3.0", "sequelize": "^3.24.6" } } ```
mdibaiee commented 2017-02-04 18:44:01 +00:00 (Migrated from github.com)

@lmd2692 You are right, it was a bug, I published the new version as hapi-sequelize-crud@2.9.2.

@lmd2692 You are right, it was a bug, I published the new version as `hapi-sequelize-crud@2.9.2`.
mdibaiee commented 2017-02-04 18:52:25 +00:00 (Migrated from github.com)

@joeybaker I just saw your commit 83eadf0929 and reverted my commit to respect yours.

The problem was that the forEach loop was processing join table models as well (e.g. TeamPlayers) which would cause duplicate CRUD creation for models. This seems to fix the issue.

@joeybaker I just saw your commit https://github.com/mdibaiee/hapi-sequelize-crud/commit/83eadf0929a2506541e34b4a6f73d46bfa2404d3 and reverted my commit to respect yours. The problem was that the forEach loop was processing join table models as well (e.g. `TeamPlayers`) which would cause duplicate CRUD creation for models. This seems to fix the issue.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: thereadme/hapi-sequelize-crud#37
No description provided.