从SequelizeJS中的表中选择特定属性
Select specific attributes from table in SequelizeJS
这些是我的表(不包括所有列)和关系
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 });
我只想从task获得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 = ?
您可以像这样查询
clients.findById(1, {
attributes: ['id'],
include: [{
model: tasks,
attributes: ['name'],
required: false
}]
}).then(function(client) {
return client.tasks.map(function(task) {
return {
name: task.name,
value: task.clients_tasks.value
};
});
}).then(function(result) {
console.log(result);
// The rest of you logic here...
});
相关文章:
- jQuery最近父级的数据属性选择器
- javascript对象操作:根据指定条件选择属性
- Javascript:从数组中的对象中选择属性
- 如何在 javascript 中为没有值的标签选择属性
- jQuery按名称选择属性只选择第一个匹配的元素
- 使用 Angular 从服务器端生成的下拉列表中获取选择属性值
- 如何在 Javascript 中从对象中选择属性
- 选择属性在更改选择元素后未更改
- jQuery - 选择属性名称(非值)以 .. 开头的所有元素
- j查询选择属性
- 选择“属性的一部分-JQuery”
- 检查是否下拉'的第一个选项已选择属性
- JQuery选择属性范围
- jquery-attr选择器不会选择属性的全部值
- 强制dijit.form.ComboBox的选择属性
- 选择属性设置为特定值的元素中的所有元素
- 使用& # 39;这个# 39;选择属性,然后与选定的HTML属性进行比较
- 按掩码选择属性
- 在Chrome上XPath选择属性失败
- amCharts选择属性值