【问题标题】:Sequelize - How to apply Distinct to nested include?Sequelize - 如何将 Distinct 应用于嵌套包含?
【发布时间】:2018-02-21 19:34:48
【问题描述】:

我正在使用 Nodejs、Express、Postgresql 和 Sequelize

我有 4 个模型

  1. 帐户(有很多书)

  2. 书(有很多部分)

  3. 部分(有很多章节)
  4. 章节

我要退回一本书,其中包括“部分”和“章节”以及一些特定的顺序 - 这很好用。

“章节”模型有一个“主题”列,我只想包含“章节”模型中具有不同“主题”的行。

如何通过这种嵌套的包含结构将 distinct 应用到这个“章节”模型?

代码sn-p:

return Book

.findOne({
    where: {accountid: req.body.accountid,
        id: req.body.bookid}, 
    include: [{
        model: Part,
        as: 'parts'
        include: [
            {
                model: Chapter,
                required: false,
                as: 'chapters',
                where: {
                    createdAt: {
                        $gte: moment().subtract(24,'hours').format()
                    }
                }
            }],
    }],
    order: [
        [
            { model: Part, as: 'parts' },
            'createdAt',
            'DESC'
        ],
        [
            { model: Part, as: 'parts' },
            { model: Chapter, as: 'chapters' },
            'createdAt',
            'DESC'
        ]
    ]


})
.then(book => res.status(200).send(book))
.catch(error => res.status(400).send(error.toString()));
}

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    Sequelize distinct 功能在包含的情况下不起作用。

    您可以使用 sequelize 原始查询来实现您的结果。

    【讨论】:

      【解决方案2】:

      我知道这是一个非常晚的答案,但实际上您可以在包含表列上有所不同。唯一的缺点是您必须分析 Sequelize 生成的查询并包含每个模型的属性。

      您的查询最终会是这样的:

      Book.findOne({
          // Literal DISTINCT ON as 1 so it is not included in the resulting json
          // '*' so all table attributes are included in the json response
          attributes: [Sequelize.literal('DISTINCT ON("chapters"."topics") 1'), '*']
          where: {accountid: req.body.accountid,
              id: req.body.bookid}, 
          include: [{
              model: Part,
              as: 'parts'
              include: [
                  {
                      model: Chapter,
                      required: false,
                      as: 'chapters',
                      where: {
                          createdAt: {
                              $gte: moment().subtract(24,'hours').format()
                          }
                      }
                  }],
          }],
          order: [
              // This order criteria is required because we are using distinct on
              [
                  { model: Chapter, as: 'chapters' },
                  'topics',
                  'ASC'
              ],
              [
                  { model: Part, as: 'parts' },
                  'createdAt',
                  'DESC'
              ],
              [
                  { model: Part, as: 'parts' },
                  { model: Chapter, as: 'chapters' },
                  'createdAt',
                  'DESC'
              ]
          ]
      
      
      })
      

      同样的策略可用于对多个列进行“区分”,但您必须记住将列作为第一个搜索条件。
      在 Sequelize 的 Github 上搜索“DISTINCT ON”会导致一些有趣的帖子。

      希望对您有所帮助。来自智利的问候。

      【讨论】:

        猜你喜欢
        • 2017-08-27
        • 2021-02-09
        • 2021-05-01
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2021-08-05
        相关资源
        最近更新 更多