续接 where 语句 foreign table

Sequelize where statement foreign table

我有以下设置:

var Module = sequelize.define('module', {
    id: DataTypes.INTEGER,
    name: DataTypes.STRING,
    description: DataTypes.STRING,
    category_id: DataTypes.STRING,
    module_type_id: DataTypes.STRING,
    gives_score: DataTypes.INTEGER,
    duration: DataTypes.STRING,
    price: DataTypes.STRING

}, {
    freezeTableName: true,})

Organization_has_module

Organization_has_module = sequelize.define('organization_has_module', {
    id: DataTypes.INTEGER,
    module_id: DataTypes.INTEGER,
    organization_id: DataTypes.INTEGER
},{freezeTableName:true});


Module.hasMany(Organization_has_module, {foreignKey: 'module_id'});

现在我正在尝试查找 Organization_has_module.organization_id = 1

中的所有模块

但是我不太确定该怎么做

我有以下代码:

Module.findAll({include: [{ all: true }],where: {module_type_id: type_id}})
.success(onSuccess).error(onError);

我认为这与您的其他问题是同一个问题?

.hasMany() 没有引用 N:M 关系,而是 1:M。由于加入 table.

,您需要 .belongsToMany() 来引用 N:M
Organization.belongsToMany(Module, {
    through: Organization_has_module,
    as: 'module',
    foreignKey: 'module_id'
})
Module.belongsToMany(Organization, {
    through: Organization_has_module,
    as: 'organization',
    foreignKey: 'organization_id'
})