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

我要在不同的表中找到一个字段,即我要检查搜索是否在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”。(以及所有相关字段的此错误。)


我能为那工作做些什么?


人到中年有点甜
浏览 335回答 1
1回答

呼唤远方

解决了!经过大量测试后,我发现错误无关,也不在 OR 中,在 LIMIT 中。搜索后我解决了这个添加duplicating: false包括。最后的代码是这样的:    var result = await CustomerModel.findAll({        include: [            {model : ShopModel, require: true, duplicating: false},            {model: ContactPersonModel, require: true, duplicating: false}        ],        ...params,        raw    })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript