sequelize连表include查询问题
发布于 7 年前 作者 zhang962976642 11893 次浏览 来自 问答

我有个疑问请教下cnodejs的诸位。
这是我的server表:
image.png
这是我的cpu表:
image.png
讲道理此时我通过server表include查询 应该可以查到两条数据吧,可是现在只有一条数据
image.png
请教下这个问题如何解决,怎么拿到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

9 回复

查了资料,完全没头绪

多对多关系可以建一个关联表,你的has_cpu字段应该使用来记录这个server有哪些cpu的吧,不如建一个server_cpu_relate

    models['cpu'].hasMany(Server, {
      foreignKey: 'cpuid',
	  through: 'server_cpu_relate'
	  ...
    });
	models['server'].hasMany(Cpu, {
      foreignKey: 'serverid',
	  through: 'server_cpu_relate'
	  ...
    });

可以参考这篇文章 传送门

@noraincode 您的意思是,我server has_cpu字段存的是中间表 cpu_project表的一个key吗,通过这个key我在查到具体的cpu信息, 那么是不是我维护的话就是需要维护多个表把,目前来说考虑过这种方法,但是比较坑的需求是,我has_cpu、has_memory、has_hard_disk ip_address、network都是类似的组合。

@zhang962976642 我的意思是不用 has_cpu 字段来记录server有多少个cpu,用下面这种关联表的方式 image.png 同样可以记录server 1 有两个cpu, 分别是1和2,server2 有一个cpu,id是1 关联表不需要维护,只要把关系搞清楚一般不会有问题,你原来有多少表还是得维护多少表 不是很清楚你的需求,只是提个思路吧,我在做一些业务的时候一般是这样处理简单的多对多关系的

@noraincode 好的,了解了,谢谢!

遇到同样的问题,类似表结构 用户组表:user_group untitled1.png 资源表:resources untitled2.png 试了这样的关联关系配置可以正确查询出数据,之前只定义hasMany关联关系时只能查出一条关联的资源数据 options[‘include’] = [{ association: userGroupSchema.hasMany(resourcesSchema, {foreignKey: ‘id’, sourceKey: ‘resource_id’}), // FIXME 通过自定义on,覆盖默认的 ON 条件: LEFT OUTER JOIN resources AS resources ON user_group.resource_id = resources.id on: db.literal(‘instr(user_group.resource_id, resources.id) > 0’) }] sql: SELECT user_group.*, resources.id AS resources.id, resources.name AS resources.name, resources.icon AS resources.icon, resources.title AS resources.title, resources.parent_id AS resources.parent_id, resources.type AS resources.type, resources.enable AS resources.enable, resources.sidebar AS resources.sidebar, resources.create_time AS resources.create_time, resources.update_ti me AS resources.update_time FROM (SELECT user_group.id, user_group.title, user_group.description, user_group.status, user_group.resource_id, user_group.create_user_id, user_group.create_time, user_ group.update_time FROM user_group AS user_group WHERE user_group.status IN (‘0’, ‘1’) ORDER BY user_group.id ASC LIMIT 0, 10) AS user_group LEFT OUTER JOIN resources AS resources ON instr(user_group.resource_ id, resources.id) > 0 ORDER BY user_group.id ASC; response: untitled3.png

这个问题最后是如何解决的呀??

db.literal(‘instr(user_group.resource_id, resources.id) > 0’) 这段中instr存在模糊查询,在精确查询时存在BUG。 可以改成下面这样: db.literal(‘instr(concat(’,’, user_group.resource_id, ‘,’), concat(’,’, resources.id, ‘,’)) > 0’)

回到顶部