在外部表中声明外部表
我有以下设置:
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'});
现在我试图findOrganization_has_module.organization_id = 1
所有模块
但我不太清楚如何去做
我有以下代码:
Module.findAll({include: [{ all: true }],where: {module_type_id: type_id}}) .success(onSuccess).error(onError);
我认为这与你的其他问题是一样的问题?
Sequelize hasMany通过另一个表
.hasMany()
不引用N:M
关系,而是它的1:M
。 由于您的连接表,您需要.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' })