{"action":"created","issue":{"url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/20","repository_url":"https://api.github.com/repos/trailsjs/trailpack-sequelize","labels_url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/20/labels{/name}","comments_url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/20/comments","events_url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/20/events","html_url":"https://github.com/trailsjs/trailpack-sequelize/issues/20","id":160266701,"number":20,"title":"Footprint createAssociation does not work correctly.","user":{"login":"cesar2064","id":9759315,"avatar_url":"https://avatars.githubusercontent.com/u/9759315?v=3","gravatar_id":"","url":"https://api.github.com/users/cesar2064","html_url":"https://github.com/cesar2064","followers_url":"https://api.github.com/users/cesar2064/followers","following_url":"https://api.github.com/users/cesar2064/following{/other_user}","gists_url":"https://api.github.com/users/cesar2064/gists{/gist_id}","starred_url":"https://api.github.com/users/cesar2064/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cesar2064/subscriptions","organizations_url":"https://api.github.com/users/cesar2064/orgs","repos_url":"https://api.github.com/users/cesar2064/repos","events_url":"https://api.github.com/users/cesar2064/events{/privacy}","received_events_url":"https://api.github.com/users/cesar2064/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2016-06-14T19:42:07Z","updated_at":"2016-06-15T19:39:27Z","closed_at":null,"body":"I have the following models:\r\nUser.js:\r\n```\r\nmodule.exports = class User extends Model {\r\n  static config (app,Sequelize) {\r\n    return {\r\n       options: {\r\n         hooks:{\r\n          beforeCreate: (values,options) =>{\r\n            return app.services.Utils.createBcryptHash(values.password).then(\r\n              (hash)=>{\r\n                values.password = hash\r\n              },\r\n              (err)=>{\r\n                app.log.error(err)\r\n              }\r\n            )\r\n          }\r\n         },\r\n         instanceMethods:{\r\n          toJSON: function () {\r\n            let $user = this.dataValues;\r\n            delete $user.password;\r\n            return $user;\r\n          }\r\n         },\r\n         classMethods: {\r\n           //If you need associations, put them here\r\n           associate: (models) => {\r\n             //More information about associations here : http://docs.sequelizejs.com/en/latest/docs/associations/\r\n             models.User.belongsToMany(models.Role, {\r\n               as: 'roles',\r\n               onDelete: 'CASCADE',\r\n               through:{\r\n                model: models.UserRole,\r\n                unique:false\r\n               },\r\n               foreignKey: \"user_id\"\r\n             })\r\n           }\r\n         }\r\n       }\r\n     }\r\n  }\r\n\r\n  static schema (app,Sequelize) {\r\n    return {\r\n      name: {\r\n        type: Sequelize.STRING,\r\n        allowNull: true,\r\n        unique : true\r\n      },\r\n      email:{\r\n        type: Sequelize.STRING,\r\n        allowNull: false,\r\n        unique : true,\r\n        validate:{\r\n          isEmail : true\r\n        }\r\n      },\r\n      external:{\r\n        type: Sequelize.BOOLEAN\r\n      },\r\n      password: {\r\n        type : Sequelize.STRING,\r\n        allowNull: false\r\n      },\r\n      displayName: {\r\n        type : Sequelize.STRING,\r\n        allowNull: true\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nRole.js\r\n\r\n```\r\nmodule.exports = class Role extends Model {\r\n\r\n  static config () {\r\n    return {\r\n       options: {\r\n         classMethods: {\r\n           //If you need associations, put them here\r\n           associate: (models) => {\r\n             //More information about associations here : http://docs.sequelizejs.com/en/latest/docs/associations/\r\n             models.Role.belongsToMany(models.User, {\r\n               as: 'users',\r\n               onDelete: 'CASCADE',\r\n               through:{\r\n                model: models.UserRole,\r\n                unique:false\r\n               },\r\n               foreignKey: \"role_id\"\r\n             })\r\n           }\r\n         }\r\n       }\r\n     }\r\n  }\r\n\r\n  static schema (app,Sequelize) {\r\n    return {\r\n      name: {\r\n        type: Sequelize.STRING,\r\n        allowNull: false,\r\n        unique:true\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nUserRole.js\r\n\r\n```\r\nmodule.exports = class UserRole extends Model {\r\n\r\n  static config () {\r\n  }\r\n\r\n  static schema (app,Sequelize) {\r\n    return {\r\n      role_id: {\r\n        type: Sequelize.INTEGER,\r\n        references: {\r\n          model: 'role',\r\n          key: 'id'\r\n        },\r\n        allowNull: false\r\n      },\r\n      user_id:{\r\n        type: Sequelize.INTEGER,\r\n        references:{\r\n          model: 'user',\r\n          key: 'id'\r\n        },\r\n        allowNull: false\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nwhen I access /POST /api/v1/user/1/roles,  body: {\"name\":\"role\"}\r\n\r\nThe application creates the role with name:role, but does not create the association (UserRole). This is the orm output:\r\n\r\n```\r\nExecuting (b68a81ff-1f56-4c0e-bdb7-9c3be8215ec8): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;\r\nExecuting (b68a81ff-1f56-4c0e-bdb7-9c3be8215ec8): INSERT INTO \"role\" (\"id\",\"name\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,'role','2016-06-14 19:32:25.273 +00:00','2016-06-14 19:32:25.273 +00:00') RETURNING *;\r\nExecuting (b68a81ff-1f56-4c0e-bdb7-9c3be8215ec8): UPDATE \"user\" SET \"updatedAt\"='2016-06-14 19:32:25.289 +00:00' WHERE \"id\" = '1'\r\nExecuting (b68a81ff-1f56-4c0e-bdb7-9c3be8215ec8): COMMIT;\r\n```\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/comments/226296974","html_url":"https://github.com/trailsjs/trailpack-sequelize/issues/20#issuecomment-226296974","issue_url":"https://api.github.com/repos/trailsjs/trailpack-sequelize/issues/20","id":226296974,"user":{"login":"cesar2064","id":9759315,"avatar_url":"https://avatars.githubusercontent.com/u/9759315?v=3","gravatar_id":"","url":"https://api.github.com/users/cesar2064","html_url":"https://github.com/cesar2064","followers_url":"https://api.github.com/users/cesar2064/followers","following_url":"https://api.github.com/users/cesar2064/following{/other_user}","gists_url":"https://api.github.com/users/cesar2064/gists{/gist_id}","starred_url":"https://api.github.com/users/cesar2064/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cesar2064/subscriptions","organizations_url":"https://api.github.com/users/cesar2064/orgs","repos_url":"https://api.github.com/users/cesar2064/repos","events_url":"https://api.github.com/users/cesar2064/events{/privacy}","received_events_url":"https://api.github.com/users/cesar2064/received_events","type":"User","site_admin":false},"created_at":"2016-06-15T19:39:27Z","updated_at":"2016-06-15T19:39:27Z","body":"Yes, but why is necessary to indicate the middleTable?"}}
{"actor":{"display_login":"cesar2064"}}