Node.js 延续多对多关系
我需要一些帮助来让我的头脑了解多对多关系的续集 我必须为模特、客户和服务多对多。现在我想通过客户端id加载一个客户端提供的所有服务。包含的条件如何才能实现这一点 服务:Node.js 延续多对多关系,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我需要一些帮助来让我的头脑了解多对多关系的续集 我必须为模特、客户和服务多对多。现在我想通过客户端id加载一个客户端提供的所有服务。包含的条件如何才能实现这一点 服务: var Service = sequelize.define('service', { title: { type: DataTypes.STRING(200), allowNull: false } },{ paranoid: true, underscored: t
var Service = sequelize.define('service', {
title: {
type: DataTypes.STRING(200),
allowNull: false
}
},{
paranoid: true,
underscored: true,
classMethods: {
associate:function(models){
Service.belongsToMany(models.client, { through: 'client_services' });
Service.hasMany(models.rule, { foreignKey: 'service_id' })
}
}
});
客户:
var Client = sequelize.define('client', {
title: {
type: DataTypes.STRING(200),
allowNull: false
},
company: {
type: DataTypes.STRING(200),
allowNull: false
},
vendor: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
consumer: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
},
address_id: {
type: DataTypes.INTEGER,
allowNull: true
}
},{
paranoid: true,
underscored: true,
classMethods: {
associate:function(models){
Client.hasMany(models.service, { through: 'client_services'});
}
}
});
在“我的控制器不工作”中,错误:[错误:客户端未与服务关联!]:
var condition = {
where: { 'client.id': req.user.client_id },
include: [{ model: Client, as: 'client' }]
}
Service.findAll(condition)
.then(responseWithResult(res))
好的,where子句需要包含在模型中:
var condition = {
include: [{ model: Client, where: { 'id': req.user.client_id } }]
}
Service.findAll(condition)
.then(responseWithResult(res))
尝试使用hasman和dbelongstothx作为答案。你能给这些文件贴个链接吗?真的有用吗?我刚刚在sequelize Github repo的一些代码中找到了对Hasman和Dbelongsto的引用,但在任何文档中都找不到对它的引用。我再看一次。你不是说有可能是属于我的吗?