如何运行rank function在给定的续集模型上生成属性?
例如,给定模型:
Players.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false
},
rating: type: DataTypes.FLOAT
},
{ sequelize }
)
我想在运行rank
时获得属性Players.getAll({ rank: rating })
。这将生成类似于以下内容的SQL:
SELECT id, rating, RANK() OVER (ORDER BY rating DESC) as rank FROM players
我该如何使用续集来做到这一点?
答案 0 :(得分:1)
您可以像下面那样使用Sequelize Literal Function-
attributes: [
'id', 'rating',
[Sequelize.literal('(RANK() OVER (ORDER BY rating DESC))'), 'rank']
]
创建新属性。