9134760
我有个疑问请教下cnodejs的诸位。
这是我的server表:
这是我的cpu表:
讲道理此时我通过server表include查询 应该可以查到两条数据吧,可是现在只有一条数据
请教下这个问题如何解决,怎么拿到cpu表的id为1,2的数据
// 这是server表关联模型
models['cpu'].hasMany(Server, {
foreignKey: 'id',
constraints: false
});
// 这是cpu表关联表模型
models['server'].hasMany(Cpu, {
foreignKey: 'id',
constraints: false,
targetKey: 'has_cpu'
});
// 查询方法如下
models['server'].findAndCountAll({
'attributes': ['id','server_node_id','.....'],
'include': [
{ 'model': models['cpu'] }
]
})
查询结果如下
{
"code": 1,
"statusCode": 200,
"data": {
"count": 1,
"rows": [
{
"id": 1,
"by_server_box": "1",
"server_id": "J001A",
"usage": "1",
"mark": false,
"has_cpu": "1,2",
"has_memory": "2",
"has_hard_disk": "1",
"bandwidth": null,
"main_board": "微星",
"network": null,
"ip_address": "1,2",
"manage_address": null,
"desc": null,
"create_time": "2017-09-19T13:49:40.000Z",
"server_usage": {
"id": 1,
"mark": false,
"usage": "ziyong",
"table_mark": true,
"table_comment": "服务器用途表,用来存数服务器节点的用途信息。",
"created_at": "2017-09-19T21:53:12.000Z",
"updated_at": "2017-09-19T21:53:15.000Z"
},
"cpus": [
{
"id": 1,
"by_cpu_product": 1,
"cpu_id": "cpu_001",
"cpu_hz": "2.08",
"mark": false,
"desc": null,
"table_mark": true,
"table_comment": "cpu表,用来存储我司所有的cpu,并进行编号方便统计cpu库存信息。",
"created_at": "2017-09-19T21:50:06.000Z",
"updated_at": "2017-09-19T21:50:08.000Z"
}
]
}
]
},
"message": "查询服务器节点信息完成。",
"mark": "success",
"time": 1505828648305
}
如果有更好的解决办法,不需要大幅度的该表模型的话,请大佬指点下下,谢谢!
再次谢过cnodejs的诸位
by: 2017/09/19