如何在findAll()中的sequelize中获取连接表的数组

问题描述 投票:0回答:1
  const response = await OrderDal.findAll({
  where: { buyerId: userId },
  attributes: { exclude: ['outcomeId', 'programId'] },
  include: [
    {
      model: User,
      as: 'buyerOrders',
      attributes: ['name'],
    },
    {
      model: User,
      as: 'sellerOrders',
      attributes: ['name'],
    },
    {
      model: Outcome,
      attributes: ['title'],
    },
    {
      model: ProgramOrder,
      as: 'programOrders',
      attributes: ['orderId', 'totalUnitsValue', 'CPO', 'PPO'],
      include: 
        {
          model: Program,
          as: 'program',
          attributes: ['title'],
        },
    },
  ],
  nest: true,
});

这是我的 listAllOrders 查询返回所有外键作为各自的值和 ProgramOrder 表 ia 一个 Program 和 Order 的多对关联表,我想将所有的 programOrder 数据作为一个数组列出,其中所有的 programOrder 都具有相同的 order_id

这是我收到的回复

 "OrderList": [
        {
            "id": 1,
            "buyerId": 1,
            "sellerId": 2,
            "channelPartnerId": 1,
            "updatedById": null,
            "orderNumber": 123,
            "status": "random_status",
            "settlementDate": "2024-02-19T07:32:01.641Z",
            "archiveDate": "2024-02-19T07:32:01.641Z",
            "serviceFee": 1.4,
            "total": 785,
            "orderDate": "2024-02-19T07:32:01.641Z",
            "invoiceDoc": "random_invoice_doc",
            "noOfOutcomes": 7257,
            "createdAt": "2024-02-19T07:32:01.641Z",
            "updatedAt": "2024-02-19T07:32:01.641Z",
            "buyerOrders": {
                "name": "Numaira"
            },
            "sellerOrders": {
                "name": "SELLER"
            },
            "outcome": {
                "title": "Culture of Innovation"
            },
            "programOrders": {
                "orderId": 1,
                "totalUnitsValue": 200,
                "CPO": 0.5,
                "PPO": "0.6",
                "program": {
                    "id": 1,
                    "title": "Quality Education for Refugees"
                }
            }
        },
     ]

我想要这种类型的回复

"OrderList": [
                {
                    "id": 1,
                    "buyerId": 1,
                    "sellerId": 2,
                    "channelPartnerId": 1,
                    "updatedById": null,
                    "orderNumber": 123,
                    "status": "random_status",
                    "settlementDate": "2024-02-19T07:32:01.641Z",
                    "archiveDate": "2024-02-19T07:32:01.641Z",
                    "serviceFee": 1.4,
                    "total": 785,
                    "orderDate": "2024-02-19T07:32:01.641Z",
                    "invoiceDoc": "random_invoice_doc",
                    "noOfOutcomes": 7257,
                    "createdAt": "2024-02-19T07:32:01.641Z",
                    "updatedAt": "2024-02-19T07:32:01.641Z",
                    "buyerOrders": {
                        "name": "Numaira"
                    },
                    "sellerOrders": {
                        "name": "SELLER"
                    },
                    "outcome": {
                        "title": "Culture of Innovation"
                    },
                    "programOrders": [
                       {
                        "orderId": 1,
                        "totalUnitsValue": 200,
                        "CPO": 0.5,
                        "PPO": "0.6",
                        "program":[
                        {
                            "id": 1,
                            "title": "Quality Education for Refugees"
                        },{
                            "id": 2 ,
                            "title": "WOMEN Education for Refugees"
                        },
                      ],
                       {
                        "orderId": 1,
                        "totalUnitsValue": 200,
                        "CPO": 5.5,
                        "PPO": "0.9",
                        "program": [
                         {
                            "id": 2,
                            "title": "Quality Education for Refugees"
                        },
                      ]
                    }
                },
               ]
node.js sequelize.js response postgresql-9.1
1个回答
0
投票

只需转到 ProgramOrder 的模型文件并检查您的关联是否类似于以下内容:

ProgramOrder.hasMany(models.Program, { foreignKey: 'your_fkey' }); 

在 OrderDal 的模型文件中执行相同的操作。

OrderDal.hasMany(models.ProgramOrder, { foreignKey: 'your_fkey' }); 

注意:为了获取数组,您的关联中应该有 hasMany,如果是 hasOne 它将作为对象返回。

© www.soinside.com 2019 - 2024. All rights reserved.