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

时间:2016-12-01 18:51:41

标签: sql join associations subset sequelize.js

我想这样做:

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)

我该怎么做?

我的Sequelize解决方案:

模型定义:

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",   "为了"" ID" AS" order.id"," order"。" taxRate" AS" order.taxRate",   "为了"" shippingCosts" AS" order.shippingCosts"," order"。" discount"   AS" order.discount"," order"。"付费" AS" order.paid",   "为了""分派" AS" order.dispatched"," order"。" payday"如   " order.payday"," order"。" billNr" AS" order.billNr",   "为了"" createdAt" AS" order.createdAt"," order"。" updatedAt"如   " order.updatedAt"," order"。" orderCustomerId"如   " order.orderCustomerId"," order"。" billCustomerId"如   " order.billCustomerId" FROM" orderArticles" AS" orderArticles"内   加入"订单" AS"订单" ON" orderArticles"。" orderId" =" order"。" id"   AND" order"。"付费" IS NOT not NULL" orderArticles"。" discountTagId" =   ' 4' GROUP BY" order"。" id"," orderArticles"。" quantity";

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

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

1 个答案:

答案 0 :(得分: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解析结果并将其发送给客户端。