使用NodeJS进行续集不能连接有限制的表
我试图实现一个简单的查询,应该看起来像这样:
select * from property join entity_area on property.id=entity_area.entity_id and entity_area.area_id=1 where property.price>300000 limit 12
非常简单:我想获得join的结果,然后限制到12。
在Sequelize中,我使用以下函数:
return models.property.findAll( { where: ["price>=?", 300000], include: [ { model:models.entity_area, where: { area_id:1 } } ], limit:12 })
但是这个代码生成以下sql:
select property.*, entity_area.* from (select * from property where property.price>300000 limit 12) join entity_area on property.id=entity_area.entity_id and entity_area.area_id=1
这与我想要做的完全不同的逻辑,因为在生成的SQL它首先获得任何12个结果,然后尝试joinentity_area,当然随机12结果不一定匹配entity_area,所以我'没有结果回来。
请给我一个正确的做法。 属性表是非常巨大的,我必须使用“限制”,而不是得到所有的结果和切片在JavaScript中。 另外我不想开始使用原始查询。
其实我自己find了解决办法 我认为这是后续框架中的一个错误。
在node_modules / sequelize / lib / dialect / abstract / query_generator.js中有一个“selectQuery”函数,它具有以下行:
subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false
首先有一个选项subquery可以作为false传递去除子查询生成。 Sequelize文档对此没有任何评价。 但是,如果你在findAll对象中传递subQuery:false,那么它将不起作用,因为某些原因,它会像selectQuery函数一样被取消。
我尝试了这样的:
return models.property.findAll( { where: ["price>=?", 300000], include: [ { model:models.entity_area, where: { area_id:1 } } ], limit:12, subQuery:false })
仍然得到options.subQuery = undefined。
所以我不得不改变query_generator.js中的function是这样的:
subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) && options.subQuery !== false && options.doSubQuery===true
所以现在默认情况下,它不会做这个丑陋的子查询,除非我明确指定doSubQuery:true。 最后,我得到了没有限制的子查询正确的查询。
models.property.findAll( { where: [...], include: [{...}], limit:12 }, { subQuery:false })
- pipe道subprocessstdout&标准input到浏览器在node.js&browserify
- 从Meteor的另一个模板获取react nativevariables
- 定制node.js CLI工具configuration文件的build议/最佳实践:位置和命名?
- 如何使用jenkinspipe道与nvm包装插件?
- Grunt与Maven集成
- Mongoose模型的静态/方法不在“this”中保存值
- 伊斯坦布尔代码覆盖检查覆盖我的规范文件,而不是源?
- 如何注入与Server.inject模拟testinghapi
- 当明确定义globalsvariables时,得到“全局variables未定义”错误