在Sequelize中减去2个字段

Jus*_*808 1 javascript sequelize.js

到目前为止,我的查询看起来像这样...

{
    where: {
      id: event.pathParameters.picId
    },
    'include': [{
      'model': db.reputations,
      'where': {
        'type': 'V_UP'
      },
      'as': 'up_reputations',
      'required': false
    }, {
      'model': db.reputations,
      'where': {
        'type': 'V_DONW'
      },
      'as': 'down_reputations',
      'required': false
    }],
    'attributes': [
      'id',
      'title',
      'data',
      'createdAt',
      [db.Sequelize.fn('count', db.Sequelize.col('up_reputations.type')), 'upVotes'],
      [db.Sequelize.fn('count', db.Sequelize.col('down_reputations.type')), 'downVotes']
    ]
}
Run Code Online (Sandbox Code Playgroud)

我需要的最后一个属性scoreupVotesdownVotes但是我不知道该怎么做。

Mar*_*ari 5

这应该工作:

'attributes': [
      'id',
      'title',
      'data',
      'createdAt',
      [db.Sequelize.fn('count', db.Sequelize.col('up_reputations.type')), 'upVotes'],
      [db.Sequelize.fn('count', db.Sequelize.col('down_reputations.type')), 'downVotes'],
      [db.Sequelize.literal('(upVotes - downVotes)'), 'score']
 ]
Run Code Online (Sandbox Code Playgroud)