gpt4 book ai didi

node.js - 续写多对多关系

转载 作者:行者123 更新时间:2023-12-03 22:22:31 25 4
gpt4 key购买 nike

我需要一些帮助来解决多对多关系的 Sequelize 问题。

我必须模型、客户和服务(多对多)。现在我想加载一个客户端提供的所有服务(通过客户端 ID)。包含条件的条件如何才能实现这一点?

服务:

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

在我的 Controller 中(不起作用错误:[错误:客户端(客户端)与服务无关!]):
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))

关于node.js - 续写多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com