postgresql - Sequelize : Returning a single COUNT entry instead of every row in a nested include

标签 postgresql sequelize.js

我正在尝试从 Sequelize 中的嵌套包含中获取总行数(计数)。我在这里关注了无数问题,但似乎无法解决问题。
楷模:

  • 品牌
  • BrandProfile(别名:brand_profile)
  • BrandPageView(别名 brand_page_view )

  • 关系:
     // Brand
     Brand.hasOne(models.BrandProfile, {
       foreignKey: 'brand_id',
       as: 'brand_profile'
     })
    
     // BrandProfile
     BrandProfile.belongsTo(models.Brand, {
       foreignKey: 'brand_id',
     })
     BrandProfile.hasMany(models.BrandPageView, {
       foreignKey: 'brand_id',
       as: 'brand_page_views'
     })
    
     // BrandPageView
     BrandProfile.belongsTo(models.BrandProfile, {
       foreignKey: 'brand_id',
     })
    
    现在,当我尝试像这样正常运行查询时:
    const { count, rows } = await Brand.findAndCountAll({
      include: [
        {
          model: BrandProfile,
          as: 'brand_profile',
          include: [
            {
              model: BrandPageView,
              as: 'brand_page_views',
            },
          ],
        },
      ],
    })
    
    它返回以下内容:
    {
      id: 1,
      created_at: '2020-12-26T20:42:19.930Z',
      updated_at: '2020-12-29T20:46:58.918Z',
      deleted_at: null,
      name: 'Test brand',
      slug: 'test-brand',
      status: 'disabled',
      brand_profile: {
        created_at: '2020-12-26T20:42:19.931Z',
        about: [ [Object], [Object], [Object], [Object] ],
        cleaned_about: null,
        subtitle: 'subtitle test',
        photo: '1609477287152.jpeg',
        photo_config: { scale: '1.00' },
        id: 1,
        updated_at: '2021-01-01T05:01:27.414Z',
        brand_id: 1,
        brand_page_views: [
          [Object], [Object],
          [Object], [Object],
          [Object], [Object],
          [Object]
        ]
      },
    }
    
    如您所见,brand.brand_profile.brand_page_views 包含所有对象的列表。现在我只想返回计数,所以我使用以下查询:
    const { count, rows } = await Brand.findAndCountAll({
      include: [
          {
            model: BrandProfile,
            as: 'brand_profile',
            include: {
              model: BrandPageView,
              as: 'brand_page_views',
              attributes: [],
            },
          },
        ],
        attributes: {
          include: [
            [
              Sequelize.fn('COUNT', Sequelize.col('brand_profile.brand_page_views.brand_id')),
              'brand_page_views_count',
            ],
          ],
        },
        group: ['brand.id']
    })
    
    我收到此错误:
     'missing FROM-clause entry for table "brand"'
    
    它输出的 SQL 是:
    SELECT "Brand"."id", "Brand"."created_at", "Brand"."updated_at", "Brand"."deleted_at", "Brand"."name", "Brand"."slug", "Brand"."status", COUNT("brand_profile->brand_page_views"."brand_id") AS "brand_profile.brand_page_views.count", "brand_profile"."created_at" AS "brand_profile.created_at", "brand_profile"."about" AS "brand_profile.about", "brand_profile"."cleaned_about" AS "brand_profile.cleaned_about", "brand_profile"."subtitle" AS "brand_profile.subtitle", "brand_profile"."photo" AS "brand_profile.photo", "brand_profile"."email" AS "brand_profile.email", "brand_profile"."photo_config" AS "brand_profile.photo_config", "brand_profile"."id" AS "brand_profile.id", "brand_profile"."updated_at" AS "brand_profile.updated_at", "brand_profile"."brand_id" AS "brand_profile.brand_id" FROM "brands" AS "Brand" LEFT OUTER JOIN "brand_profile" AS "brand_profile" ON "Br2021-01-05 01:32:26 [sequelize] INFO   Executing (default): SELECT "Brand"."id", "Brand"."created_at", "Brand"."updated_at", "Brand"."deleted_at", "Brand"."name", "Brand"."slug", "Brand"."status", COUNT("brand_profile->brand_page_views"."brand_id") AS "brand_profile.brand_page_views.count", "brand_profile"."created_at" AS "brand_profile.created_at", "brand_profile"."about" AS "brand_profile.about", "brand_profile"."cleaned_about" AS "brand_profile.cleaned_about", "brand_profile"."subtitle" AS "brand_profile.subtitle", "brand_profile"."photo" AS "brand_profile.photo", "brand_profile"."email" AS "brand_profile.email", "brand_profile"."photo_config" AS "brand_profile.photo_config", "brand_profile"."id" AS "brand_profile.id", "brand_profile"."updated_at" AS "brand_profile.updated_at", "brand_profile"."brand_id" AS "brand_profile.brand_id" FROM "brands" AS "Brand" LEFT OUTER JOIN "brand_profile" AS "brand_profile" ON "Brand"."id" = "brand_profile"."brand_id" LEFT OUTER JOIN "brand_page_views" AS "brand_profile->braand"."id" = "brand_profile"."brand_id" LEFT OUTER JOIN "brand_page_views" AS "brand_profile->brand_page_views" ON "brand_profile"."id" = "brand_profile->brand_page_views"."brand_id" WHERE "Brand"."id" = 1 GROUP BY "brand"."id";
    nd_page_views" ON "brand_profile"."id" = "brand_profile->brand_page_views"."brand_id" WHERE "Brand"."id" = 1 GROUP BY "brand"."id";
    
    老实说,我已经尝试了很多方法,将 brand_profile->brand_page_views.brand_idbrand_profile.brand_id 添加到 group ,但无济于事。

    最佳答案

    Sequelize 并非旨在支持多种 SQL 聚合。因此,在您的情况下,您应该将 sequelize.literal 与子查询一起使用来计算子记录:

    attributes: {
          include: [
            [Sequelize.literal('(select COUNT(*) from brand_page_views where brand_page_views.brand_id=brand_profile.id)'),
              'brand_page_views_count'],
          ],
        },
    
    不要忘记删除 group: ['brand.id']

    关于postgresql - Sequelize : Returning a single COUNT entry instead of every row in a nested include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65576339/

    相关文章:

    mysql - SequelizeEagerLoadingError : associated not working in sequelize

    javascript - Sequelize多对多关系同一张表

    sql - postgresql 9.2中,分区可以使用hint优化吗?

    postgresql - 如何在 Postgres 表上存储 Go 的当前时间(小时-分钟-秒)?

    postgresql - 无法启动服务器,可能是由于 .conf 放错位置。如何验证原因并查找/替换 .conf?

    mysql - 如何检索没有具有特定值的子对象的父对象? Sequelize

    php - 如何将 PostgreSql 与 PHP 结合起来?

    sequelize.js - 记录 Sequelize 迁移

    javascript - Sequelize 文档中的这些井号/井号符号代表什么?

    node.js - 从 sequelize 钩子(Hook)返回不同的结果