更新时间:2023-02-06 19:36:51
其实我自己找到了解决方案.我认为这是 sequelize 框架中的一个错误.
在 node_modules/sequelize/lib/dialect/abstract/query_generator.js 中有一个selectQuery"函数,它具有以下行:
Actually I found a solution myself. I think this is a bug in sequelize framework.
In the node_modules/sequelize/lib/dialect/abstract/query_generator.js there is a "selectQuery" function which has the following line:
subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false
首先有一个选项 subQuery 可以传递为 false 以删除子查询生成.Sequelize 文档对此一无所知.但是此外,如果您在 findAll 对象中传递 subQuery:false 它将无法工作,因为由于某种原因它变得与 selectQuery 函数一样不够完善.
我试过类似的东西:
First of all there is an option subQuery that could be passed as false to remove the subquery generation. Sequelize documentation does not have a word about it. But moreover if you pass subQuery:false in the findAll object it's not going to work because for some reason it's getting as underfined to the selectQuery function.
I tried something like:
return models.property.findAll(
{
where: ["price>=?", 300000],
include: [
{
model:models.entity_area,
where: { area_id:1 }
}
],
limit:12,
subQuery:false
})
仍然有 options.subQuery=undefined.
and still got options.subQuery=undefined.
所以我不得不将 query_generator.js 中的函数更改为类似:
So i had to change the function in query_generator.js to be something like:
subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false && options.doSubQuery===true
所以现在默认情况下它不会执行这个丑陋的子查询,除非我明确指定 doSubQuery:true.最后我得到了正确的查询,没有带限制的子查询.
So now by default it's not doing this ugly subquery unless i specify explicitely doSubQuery:true. And finally i got the proper query without subquery with limit.