这些是我的表(不包括所有列)和关系
var client = schema.define('client', {
name: {
type: Sequelize.STRING,
allowNull: false
},
}
var task = schema.define('task', {
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
description: {
type: Sequelize.STRING,
}
}
var clientTask = schema.define('clientTask', {
value: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: false
},
}
client.belongsToMany(task, { through: clientTask });
task.belongsToMany(client, { through: clientTask });
我想从任务中获取name
,从clientTask获取value
,我按客户端ID搜索,这是我到目前为止所尝试的内容。
client.findAll({
attributes: [],
where: {id: clientId},
include: [{
model: task,
attributes: ['name']
}]
}).then(function (clients) {
//client.tasks is array with task objects(models) with only name attribute
//client.tasks[0].clientTask is object(models) with all attributes but I want only `value`
}
基本上我想要的是这个查询
Select
tasks.name,
clienttasks.value
From
clients Inner Join
clienttasks
On clienttasks.clientId = clients.id Inner Join
tasks
On clienttasks.taskId = tasks.id
Where clients.id = ?