为了账号安全,请及时绑定邮箱和手机立即绑定

使用 OR 对多表关联查询进行 Sequelize

使用 OR 对多表关联查询进行 Sequelize

人到中年有点甜 2021-09-17 17:24:44
我要在不同的表中找到一个字段,即我要检查搜索是否在Customer表的不同字段、Shops表的不同字段和ContactPerson表的不同字段中Customer 和 Persons 以及 Customer Shops 表是 1-N 并且相关联我的“搜索”变量是我想要找到的。这是我的代码CustomerPersonsRelation()CustomerShop()    var result = await CustomerModel.findAll({        ...params,        include: [            {model : ShopModel},            {model: ContactPersonModel}        ]    })都是进口的,不用担心。我的参数是这些:{        where: {            $or: [                {                    '$Customer.customer_name$': { $like: '%' + search + '%' },                },                {                    '$Customer.customer_fiscal_name$': { $like: '%' + search + '%' },                },                {                    '$Customer.customer_responsable_name$': { $like: '%' + search + '%' },                },                {                    '$Customer.customer_responsable_phone$': { $like: '%' + search + '%' },                },                {                    '$Customer.customer_web$': { $like: '%' + search + '%' },                },                {                    '$Shops.shop_name$': { $like: '%' + search + '%' },                },                {                    '$Shops.shop_phone$': { $like: '%' + search + '%' },                },                {                    '$Shops.shop_mobile$': { $like: '%' + search + '%' },                },                {                    '$ContactPersons.contactp_name$': { $like: '%' + search + '%' },                },                {                    '$ContactPersons.contactp_phone$': { $like: '%' + search + '%' },                },                {                    '$ContactPersons.contactp_mobile$': { $like: '%' + search + '%' },                }            ]        },        limit: 3    })}但这会返回此错误:错误:SequelizeDatabaseError:无法绑定多部分标识符“ContactPersons.contactp_mobile”。(以及所有相关字段的此错误。)我能为那工作做些什么?
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

解决了!


经过大量测试后,我发现错误无关,也不在 OR 中,在 LIMIT 中。


搜索后我解决了这个添加duplicating: false包括。


最后的代码是这样的:


    var result = await CustomerModel.findAll({

        include: [

            {model : ShopModel, require: true, duplicating: false},

            {model: ContactPersonModel, require: true, duplicating: false}

        ],

        ...params,

        raw

    })


查看完整回答
反对 回复 2021-09-17
  • 1 回答
  • 0 关注
  • 334 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号