sequelize关联查询时的分页问题,join,limit

用到许多数据库关系化映射中间件,hibernate,jpa,iBATIS,最近研究nodejs,发现一款不可多得的orm开源工具sequelize,支持promise,映射配置/查询/数据输出等都是json格式,很是顺心,官方文档很标准但彻底说不透其强大的功能,不少都须要实际用到才能体会,就像json同样变化无穷,你猜不透它有多少种变化。node

好,下面来看一个需求案例:
一条这样的普通查询语句:sql

select * from product join producton product.id=place.productid and place.city=1100 where product.price>100 limit 10

用sequelize的query来写,若是写成这样:数据库

models.product.findAll({
    where: ["price>=?", 100 ],
    include: [{
        model:models.product,
        where: { city:1100 }
    }],
    limit:12
})

实际上运行的sql是这个:json

select product.*, place.* from (select * from product where product.price>100 limit 10) join place on product.id=place.productid and place.city=1100

想要的结果是错误的,分页时没有把city:1100 条件限制了,结果有差别,那怎么办?promise

因而找方法,看到有人使用加subQuery:false条件来处理,以下:工具

models.product.findAll({
    where: ["price>=?", 100 ],
    include: [{
        model:models.product,
        where: { city:1100 }
    }],
    limit:10,
    subQuery:false   //不让在子查询里分页,全局处理
})

这样对于只含一个include关联的查询却是问题不大,若是include多个对象,关联的对象有1对多,多对多的关系,就很差控制了。ui

个人解决方法是,在须要一对一关联的表中加入required:true,这样就会将这个条件放在分页以前执行,hibernate

models.product.findAll({
    where: ["price>=?", 100 ],
    include: [{
        model:models.product,
        where: { city:1100 },
        required:true  //inner join方式
    }],
    limit:10,
})

运行时sql以下:code

select product.*,place.* from product join place on product.id=place.productid and place.city=1100 where product.price>100 limit 10

required参数通常是指关联对象是外联仍是内联,内联required=true能够表示内联条件优先,分页在后。
以上内容进仅供参考,使用场景不一样,理解也不同。orm

相关文章
相关标签/搜索