sequelize: belongsToMany relationship not working properly (2.0.1)

First, I am loading my models with a function:

module.exports = function(config) {
  var db, files, modelPath, myDb, sequelize;
  sequelize = new Sequelize(config.database, config.username, config.password, {
    dialect: 'postgres',
    host: config.host,
    port: config.port,
    logging: false,
    define: {
      charset: 'utf8',
      collate: 'utf8_general_ci'
    }
  });
  db = {};
  modelPath = __dirname + "/models";
  files = fs.readdirSync(modelPath);
  _.each(files, function(file) {
    var model;
    if ('.coffee' === path.extname(file)) {
      model = sequelize["import"](path.join(modelPath, file));
      return db[model.name] = model;
    }
  });
  Object.keys(db).forEach(function(modelName) {
    if ('associate' in db[modelName]) {
      return db[modelName].associate(db);
    }
  });
  myDb = _.assign(db, {
    sequelize: sequelize,
    Sequelize: Sequelize
  });
  return myDb;
};

I have 2 models that are associated:

module.exports = function(sequelize, DataTypes) {
  var User;
  User = sequelize.define('User', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    username: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        isEmail: true
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    },
    status: {
      type: DataTypes.ENUM('active', 'inactive'),
      defaultValue: 'active'
    },
    api_key: {
      type: DataTypes.STRING,
      allowNull: true
    }
  }, {
    instanceMethods: {
      display: function() {
        var user;
        user = {
          name: this.name,
          email: this.email,
          username: this.username,
          active: this.active
        };
        return user;
      }
    }
  }, {
    classMethods: {
      associate: function(models) {
        return User.belongsToMany(models.Entity, {
          through: 'UserEntity'
        });
      }
    }
  });
  User.hook('beforeCreate', function(user, options, beforeCreateCb) {
    var uniqueKey;
    uniqueKey = (JSON.stringify(user)) + " " + (new Date()) + " " + (Math.random());
    user.api_key = crypto.createHash('md5').update(uniqueKey).digest('hex');
    return beforeCreateCb(null, user);
  });
  return User;
};

and

module.exports = function(sequelize, DataTypes) {
  var Entity;
  return Entity = sequelize.define('Entity', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        return Entity.belongsToMany(models.User, {
          through: 'UserEntity'
        });
      }
    }
  });
};

I found a user via:

myDb.User.find({
  where: {
    username: 'snakamoto'
  }
}).then(function(dbUser) {
  var entities;
  should.exist(dbUser.id);
  entities = dbUser.getEntities();
  return done();
});

However, I get an error: TypeError: Object [object SequelizeInstance] has no method 'getEntities'

Not sure what I’m doing wrong. But this feels like a bug

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

@scutler19 http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html

classMethods and instanceMethods are removed.

That’s probably the reason why it doesn’t work for you. Here’s an example of the new way to do it : https://github.com/valpet/express-example/blob/2c8fd15f20cf005aa620669fa8d2fb1376d37dc4/models/task.js