{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/3118","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/3118/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/3118/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/3118/events","html_url":"https://github.com/sequelize/sequelize/issues/3118","id":57467318,"number":3118,"title":"belongsToMany relationship not working properly (2.0.1)","user":{"login":"shamoons","id":1019677,"avatar_url":"https://avatars.githubusercontent.com/u/1019677?v=3","gravatar_id":"","url":"https://api.github.com/users/shamoons","html_url":"https://github.com/shamoons","followers_url":"https://api.github.com/users/shamoons/followers","following_url":"https://api.github.com/users/shamoons/following{/other_user}","gists_url":"https://api.github.com/users/shamoons/gists{/gist_id}","starred_url":"https://api.github.com/users/shamoons/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shamoons/subscriptions","organizations_url":"https://api.github.com/users/shamoons/orgs","repos_url":"https://api.github.com/users/shamoons/repos","events_url":"https://api.github.com/users/shamoons/events{/privacy}","received_events_url":"https://api.github.com/users/shamoons/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/sequelize/sequelize/labels/bug","name":"bug","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-02-12T14:34:12Z","updated_at":"2015-02-12T15:31:58Z","closed_at":null,"body":"First, I am loading my models with a function:\r\n\r\n```javascript\r\nmodule.exports = function(config) {\r\n  var db, files, modelPath, myDb, sequelize;\r\n  sequelize = new Sequelize(config.database, config.username, config.password, {\r\n    dialect: 'postgres',\r\n    host: config.host,\r\n    port: config.port,\r\n    logging: false,\r\n    define: {\r\n      charset: 'utf8',\r\n      collate: 'utf8_general_ci'\r\n    }\r\n  });\r\n  db = {};\r\n  modelPath = __dirname + \"/models\";\r\n  files = fs.readdirSync(modelPath);\r\n  _.each(files, function(file) {\r\n    var model;\r\n    if ('.coffee' === path.extname(file)) {\r\n      model = sequelize[\"import\"](path.join(modelPath, file));\r\n      return db[model.name] = model;\r\n    }\r\n  });\r\n  Object.keys(db).forEach(function(modelName) {\r\n    if ('associate' in db[modelName]) {\r\n      return db[modelName].associate(db);\r\n    }\r\n  });\r\n  myDb = _.assign(db, {\r\n    sequelize: sequelize,\r\n    Sequelize: Sequelize\r\n  });\r\n  return myDb;\r\n};\r\n```\r\n\r\nI have 2 models that are associated:\r\n```javascript\r\nmodule.exports = function(sequelize, DataTypes) {\r\n  var User;\r\n  User = sequelize.define('User', {\r\n    name: {\r\n      type: DataTypes.STRING,\r\n      allowNull: false\r\n    },\r\n    username: {\r\n      type: DataTypes.STRING,\r\n      allowNull: false,\r\n      unique: true\r\n    },\r\n    email: {\r\n      type: DataTypes.STRING,\r\n      allowNull: false,\r\n      unique: true,\r\n      validate: {\r\n        isEmail: true\r\n      }\r\n    },\r\n    password: {\r\n      type: DataTypes.STRING,\r\n      allowNull: false\r\n    },\r\n    status: {\r\n      type: DataTypes.ENUM('active', 'inactive'),\r\n      defaultValue: 'active'\r\n    },\r\n    api_key: {\r\n      type: DataTypes.STRING,\r\n      allowNull: true\r\n    }\r\n  }, {\r\n    instanceMethods: {\r\n      display: function() {\r\n        var user;\r\n        user = {\r\n          name: this.name,\r\n          email: this.email,\r\n          username: this.username,\r\n          active: this.active\r\n        };\r\n        return user;\r\n      }\r\n    }\r\n  }, {\r\n    classMethods: {\r\n      associate: function(models) {\r\n        return User.belongsToMany(models.Entity, {\r\n          through: 'UserEntity'\r\n        });\r\n      }\r\n    }\r\n  });\r\n  User.hook('beforeCreate', function(user, options, beforeCreateCb) {\r\n    var uniqueKey;\r\n    uniqueKey = (JSON.stringify(user)) + \" \" + (new Date()) + \" \" + (Math.random());\r\n    user.api_key = crypto.createHash('md5').update(uniqueKey).digest('hex');\r\n    return beforeCreateCb(null, user);\r\n  });\r\n  return User;\r\n};\r\n```\r\n\r\nand\r\n```javascript\r\nmodule.exports = function(sequelize, DataTypes) {\r\n  var Entity;\r\n  return Entity = sequelize.define('Entity', {\r\n    name: {\r\n      type: DataTypes.STRING,\r\n      allowNull: false\r\n    }\r\n  }, {\r\n    classMethods: {\r\n      associate: function(models) {\r\n        return Entity.belongsToMany(models.User, {\r\n          through: 'UserEntity'\r\n        });\r\n      }\r\n    }\r\n  });\r\n};\r\n```\r\n\r\nI found a user via: \r\n```javascript\r\nmyDb.User.find({\r\n  where: {\r\n    username: 'snakamoto'\r\n  }\r\n}).then(function(dbUser) {\r\n  var entities;\r\n  should.exist(dbUser.id);\r\n  entities = dbUser.getEntities();\r\n  return done();\r\n});\r\n```\r\n\r\nHowever, I get an error: `TypeError: Object [object SequelizeInstance] has no method 'getEntities'`\r\n\r\nNot sure what I'm doing wrong. But this feels like a bug"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/74090153","html_url":"https://github.com/sequelize/sequelize/issues/3118#issuecomment-74090153","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/3118","id":74090153,"user":{"login":"mickhansen","id":699049,"avatar_url":"https://avatars.githubusercontent.com/u/699049?v=3","gravatar_id":"","url":"https://api.github.com/users/mickhansen","html_url":"https://github.com/mickhansen","followers_url":"https://api.github.com/users/mickhansen/followers","following_url":"https://api.github.com/users/mickhansen/following{/other_user}","gists_url":"https://api.github.com/users/mickhansen/gists{/gist_id}","starred_url":"https://api.github.com/users/mickhansen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mickhansen/subscriptions","organizations_url":"https://api.github.com/users/mickhansen/orgs","repos_url":"https://api.github.com/users/mickhansen/repos","events_url":"https://api.github.com/users/mickhansen/events{/privacy}","received_events_url":"https://api.github.com/users/mickhansen/received_events","type":"User","site_admin":false},"created_at":"2015-02-12T15:31:58Z","updated_at":"2015-02-12T15:31:58Z","body":"It's just a method on your models, if the method on each method is being called then you are calling it correctly"}}