对包含模型使用的条件进行序列化

编程入门 行业动态 更新时间:2024-10-04 03:19:37

对包含<a href=https://www.elefans.com/category/jswz/34/1771294.html style=模型使用的条件进行序列化"/>

对包含模型使用的条件进行序列化

我有两个关联的模型->订单属于阶段,我想在选择具有所选phase.projectId的订单时使用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', required: true}],
        where:{'$phase.projectId$': chosenProject.id},
        group:[
            'product_id'
        ],
        raw: true
    })

但它会创建错误 Unknown columns 'phase.projectId' in 'where Clause'

是否可以对包含的模型使用 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 子句进行内部联接(不包含

required: true
选项)。

更多推荐

对包含模型使用的条件进行序列化

本文发布于:2024-05-31 06:12:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   条件   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!