【问题标题】:Sequelize js how to get average (aggregate) of associated modelSequelize js如何获取关联模型的平均值(聚合)
【发布时间】:2020-05-12 16:28:51
【问题描述】:

我正在尝试使用 sequelize 获得模型“用户”的相关模型“评级”的平均评级。

sequelize.sync({logging: false}).then(()=>{
  return Model.Rating.findAll({
    attributes: [[Sequelize.fn('avg', Sequelize.col('stars')),'rating']]
  })
}).then(res => {
  res = res.map(r => r.get())
  console.log(res);
})

直接从“评级”模型尝试时,我得到了正确的响应:

[ { rating: '3.5000000000000000' } ]

但是,当尝试通过“用户”的关联来做同样的事情时,我会得到单独的值而不是平均值。

sequelize.sync({logging: false}).then(()=>{
  return Model.User.findOne({
    where: {id: 7},
    include : [{
      model: Model.Rating, as: 'seller_rating',
      attributes: [[Sequelize.fn('avg', Sequelize.col('stars')),'rating']]
    }],
    attributes: {
      exclude: ['password']
    },
    group: ['seller_rating.id', 'user.id'],
  })
}).then(res => {
  res = res.get()
  res.seller_rating = res.seller_rating.map(r => r.get())
  console.log(res)
})

我必须在组中添加“seller_rating.id”和“user.id”,否则 sequelize 会抛出错误。

{
  id: 7,
  email: 'example@gmail.com',
  createdAt: 2020-01-20T09:07:47.101Z,
  updatedAt: 2020-01-21T08:58:52.036Z,
  seller_rating: [
    { rating: '4.0000000000000000' },
    { rating: '3.0000000000000000' }
  ]
}

以下是用户和评分模型 用户:

let User = sequelize.define('user', {
    email: {type: Sequelize.STRING, unique: true, allowNull: false},
    password : {type: Sequelize.STRING, },
})

评分:

let Rating = sequelize.define('rating', {
    seller_id: {
        type: Sequelize.INTEGER,
        references: {model: User, key: 'id'},
        unique: 'rateObject'
    },
    buyer_id: {
        type: Sequelize.INTEGER,
        references: {model: User, key: 'id'},
        unique: 'rateObject'
    },
    stars: {
        type: Sequelize.INTEGER,
        validate: {
            min: 1,
            max: 5
        },
        allowNull: false
    }
})

Rating.belongsTo(User,{ onDelete: 'cascade', foreignKey: 'seller_id'})
Rating.belongsTo(User,{ onDelete: 'cascade', foreignKey: 'buyer_id'})
User.hasMany(Rating, { foreignKey: 'seller_id', as: 'seller_rating'})
User.hasMany(Rating, { foreignKey: 'buyer_id', as: 'buyer_rating'})

【问题讨论】:

    标签: node.js orm sequelize.js associations aggregate-functions


    【解决方案1】:

    这个解决方案应该适合你:

    Model.User.findOne({
      where: { id: 7 },
      attributes: [
        [Sequelize.fn('AVG', Sequelize.col('seller_rating.stars')), 'avgRating'],
      ],
      include: [
        {
          model: Model.Rating,
          as: 'seller_rating',
          attributes: [],
        },
      ],
      raw: true,
      group: ['User.id'],
    }).then((res) => console.log(res));
    

    【讨论】:

    • 有没有办法用聚合来做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2019-04-09
    • 2016-10-24
    相关资源
    最近更新 更多