Sequelize where condition used for included model



我有两个相关的模型->订单属于阶段,我想在选择具有所选阶段的订单时使用where子句。projectId。

models.Order.findAll( {
attributes:[
'id',
'product_id',
'phase_id',
[models.Order.sequelize.fn('sum', models.Order.sequelize.col('invoiced')), 'invoiced_quantity'],
],
include: [{model:models.Phase, as:'phase', required: true}],
where:{'$phase.projectId$': chosenProject.id},
group:[
'product_id'
],
raw: true
})

但它在"where子句"中创建了一个错误未知列"phase.projectId">

是否可以对包含的模型使用where子句?

您可以在include中使用where子句。

models.Order.findAll( {
attributes:[
'id',
'product_id',
'phase_id',
[models.Order.sequelize.fn('sum', models.Order.sequelize.col('invoiced')), 'invoiced_quantity'],
],
include: [{model:models.Phase, as:'phase', where: {id: chosenProject.id}],
group:[
'product_id'
],
raw: true
})

include中的where子句使内部联接(w/orequired: true选项(。

最新更新