首页 文章

Sequelize:在模型A的子集上,求和关联模型B的整数属性

提问于
浏览
0

I want to do this:

select sum("quantity") as "sum"
from "orderArticles" 
inner join "orders" 
on "orderArticles"."orderId"="orders"."id" 
and "orderArticles"."discountTagId" = 2
and "orders"."paid" is not null;

这导致我的数据库:

sum 
-----
 151

(1 row)

我该怎么做?

My Sequelize solution:

模型定义:

const order = Conn.define('orders', {
        id: {
            type: Sequelize.BIGINT,
            autoIncrement: true,
            primaryKey: true
        },
        // ...
        paid: {
            type: Sequelize.DATE,
            defaultValue: null
        },
        // ...
    },
// ...
})

const orderArticle = Conn.define('orderArticles', 
    {
        id: {
            type: Sequelize.BIGINT,
            autoIncrement: true,
            primaryKey: true
        },

        // ...

        quantity: {
            type: Sequelize.INTEGER,
            defaultValue: 1
        }
    },
    {
        scopes: {
            paidOrders: {
                include: [
                   { model: order, where: { paid: {$ne: null}} }
               ]
            }
        },
// ...
})

协会:

orderArticle.belongsTo(order)
order.hasMany(orderArticle, {onDelete: 'cascade', hooks: true})

经过几个小时的研究,我想出了这个:

db.models.orderArticles
 .scope('paidOrders') // select only orders with paid: {$ne: null}
 .sum('quantity', { // sum up all resulting quantities
    attributes: ['quantity'], // select only the orderArticles.quantity col
    where: {discountTagId: 2}, // where orderArticles.discountTagId = 2
    group: ['"order"."id"', '"orderArticles"."quantity"'] // don't know why, but Sequelize told me to
 })
.then(sum => sum) // return the sum

导致这个SQL:

SELECT“orderArticles” . “quantity”,sum(“quantity”)AS“sum”,“order” . “id”AS“order.id”,“order” . “taxRate”AS“order.taxRate”,“订单“ . ”shippingCosts“AS”order.shippingCosts“,”订单“ . ”折扣“AS”order.discount“,”订单“ . ”支付“AS”order.paid“,”订单“ . ”发送“AS” order.dispatched“,”order“ . ”payday“AS”order.payday“,”order“ . ”billNr“AS”order.billNr“,”order“ . ”createdAt“AS”order.createdAt“,”order“ . “updatedAt”AS“order.updatedAt”,“order” . “orderCustomerId”AS“order.orderCustomerId”,“order” . “billCustomerId”AS“order.billCustomerId”FROM“orderArticles”AS“orderArticles”INNER JOIN“命令“AS”订单“ON”orderArticles“ . ”orderId“=”order“ . ”id“AND”order“ . ”paid“IS NOT NULL WHERE”orderArticles“ . ”discountTagId“='4'GROUP BY”order“ . “id”,“orderArticles” . “数量”;

在同一数据库中有此结果: 0 rows

如果你知道我的错误,请告诉我!谢谢 :)

1 回答

  • 0

    找到解决方案:

    在orderArticle模型的范围定义中:

    scopes: {
        paidOrders: {
            include: [{ 
                model: order, 
                where: { paid: {$ne: null}}, 
                attributes: [] // don't select additional colums!
            }]
        }
    },
    //...
    

    和算法:

    db.models.orderArticles
        .scope('paidOrders')
        .sum('quantity', {
            attributes: [], // don't select any further cols
            where: {discountTagId: 2}
        })
    

    注意:在我的情况下,返回承诺就足够了 . 我使用GraphQL解析结果并将其发送到客户端 .

相关问题