Sequelize belongsToMany关系不起作用

时间:2015-02-12 14:35:36

标签: sequelize.js

首先,我正在使用函数加载我的模型:

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;
};

我有两个相关的模型:

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;
};

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'
        });
      }
    }
  });
};

我通过以下网址找到了一位用户:

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

但是,我收到错误:TypeError: Object [object SequelizeInstance] has no method 'getEntities'

不确定我做错了什么。如果重要,我正在使用2.0.1

1 个答案:

答案 0 :(得分:2)

这可能是自动复数的问题。以下续篇文档部分可能包含解决此问题的提示:

http://docs.sequelizejs.com/en/latest/docs/associations/#naming-strategy

可能你需要在协会中告诉sequelize实体的复数是什么样的,在你的情况下是'实体'。

祝你好运。

相关问题