【问题标题】:how not to count included associations in sequelize如何不计算 sequelize 中包含的关联
【发布时间】:2021-02-18 06:00:09
【问题描述】:

我在 sequelize 中有一个模型,它与两个模型相关联。我已经定义了一个控制器来查询表并包含相关模型并计算结果。本质上,父表有 7 条记录,而我的结果计数还计算了关联的模型,这并不意味着。

exports.getAllGasStocks = catchAsync(async (req, res, next) => {
  const { count, rows: results } = await GasStock.findAndCountAll({
    limit: 2,
    offset: 3,
    order: [['id', 'DESC']],

    //I don't want these fields to be counted as they being counted now
    include: ['gasVariety', 'orderedGasStocks'],  
  });

  res.status(200).json({
    count: count,
    results,
  });
});

请问,有人可以帮忙不计算代码中注释的相关字段吗?谢谢。

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    使用不同的选项:

      const { count, rows: results } = await GasStock.findAndCountAll({
        limit: 2,
        offset: 3,
        order: [['id', 'DESC']],
    
        //I don't want these fields to be counted as they being counted now
        include: ['gasVariety', 'orderedGasStocks'],
        distinct: true,  // Required for counting issue
      }); 
    

    【讨论】:

    • 所以,请标记答案以帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2021-10-28
    • 2017-07-21
    • 1970-01-01
    • 2016-10-15
    相关资源
    最近更新 更多