我可以在nodejs的sequelize中使用`SET`变量吗

时间:2018-10-11 11:50:37

标签: mysql node.js sequelize.js

我可以在nodejs的续集中使用SET变量吗?

序列化

模型

会话模型

"use strict";
module.exports = (sequelize, DataTypes) => {
  const Sessions = sequelize.define(
    "sessions",
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      challenge_id: {
        type: DataTypes.INTEGER,
        references: {
          model: "challenges",
          key: "id"
        },
        allowNull: false
      },
      createdAt: { type: DataTypes.DATE },
      createdBy: {
        type: DataTypes.INTEGER,
        references: {
          model: "users",
          key: "id"
        },
        allowNull: false
      },
      showAtLeaderboard: { type: DataTypes.ENUM("yes", "no") },
      sessionFile: { type: DataTypes.STRING },
      score: { type: DataTypes.INTEGER },
    },
    {
      timestamps: false,
      underscored: true
    }
  );
  return Sessions;
};

挑战模型

"use strict";
module.exports = (sequelize, DataTypes) => {
  const Challenges = sequelize.define(
    "challenges",
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      song_id: { type: DataTypes.INTEGER },
      challenge_name: { type: DataTypes.STRING },
      challengeDescription: { type: DataTypes.STRING },
      challengeImg: { type: DataTypes.STRING },
      challengeType: { type: DataTypes.STRING },
      coins: { type: DataTypes.INTEGER },
      createdAt: { type: DataTypes.DATE },
      expireAt: { type: DataTypes.DATE },
      isActive: { type: DataTypes.ENUM("yes", "no") },
      tags: { type: DataTypes.STRING }
    },
    {
      timestamps: false,
      underscored: true
    }
  );
  return Challenges;
};

关系

db.challenges.hasMany(db.sessions, {
  foreignKey: "challenge_id",
  sourceKey: "id"
});

我可以使用Sequelize Orm模型执行以下SQL查询吗?

SET @rank=0

SELECT `challenges`.`*`, `sessions`.*, @rank:=@rank+1 AS rank FROM `challenges` JOIN `sessions` ON `sessions`.`challenge_id` = `challenges`.`id` ORDER BY `sessions`.`score` DESC

Sequelize的ORM模型支持这种类型的查询。

0 个答案:

没有答案