如何使用 Sequelize 跨 3 个表进行分组? [英] How can I do a group by across 3 tables with Sequelize?

查看:40
本文介绍了如何使用 Sequelize 跨 3 个表进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型是:

module.exports = function(sequelize, DataTypes) {
  var CommitFileStatistic;
  return CommitFileStatistic = sequelize.define('CommitFileStatistic', {
    additions: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    deletions: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    status: {
      type: DataTypes.STRING,
      allowNull: false
    },
    fileSize: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    levenshteinDelta: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    fileHash: {
      type: DataTypes.STRING,
      allowNull: true
    }
  }, {
    classMethods: {
      associate: function(models) {
        CommitFileStatistic.belongsTo(models.Commit);
        return CommitFileStatistic.belongsTo(models.SourceFile);
      }
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  var SourceFile;
  return SourceFile = sequelize.define('SourceFile', {
    filename: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        return SourceFile.belongsTo(models.Repository);
      }
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  var Commit;
  return Commit = sequelize.define('Commit', {
    sha: {
      type: DataTypes.STRING,
      allowNull: false
    },
    commitTime: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    message: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    isParsed: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        Commit.hasMany(models.Branch);
        return Commit.hasMany(models.Commit, {
          as: 'Parent',
          through: 'ParentCommit'
        });
      }
    }
  });
};

我想做一个基本上可以做的查询:SELECT COUNT(*) AS fileCount, sf.* FROM CommitFileStatistics cfs, Commits c, SourceFiles sf WHERE cfs.CommitId = c.id AND cfs.SourceFileId =sf.id AND c.RepositoryId = 2 GROUP BY cfs.SourceFileId ORDER BY fileCount DESC 但是我想使用 ORM 而不是原始查询.这可能吗?

I want to do a query that would basically do: SELECT COUNT(*) AS fileCount, sf.* FROM CommitFileStatistics cfs, Commits c, SourceFiles sf WHERE cfs.CommitId = c.id AND cfs.SourceFileId = sf.id AND c.RepositoryId = 2 GROUP BY cfs.SourceFileId ORDER BY fileCount DESC however I want to use the ORM instead of a raw query. Is this possible?

推荐答案

这样的事情应该是您正在寻找的:

Something like this should be what you are looking for:

return CommitFileStatistic.findAll({
  attributes: [[Sequelize.fn('COUNT', '*'), 'fileCount']],
  include: [
    { model: Commit, attributes: [] },
    { model: SourceFile, attributes: [] }
  ],
  group: ['SourceFileId'],
  order: [['fileCount', 'DESC']]
});

这将导致以下查询:

SELECT 
    `CommitFileStatistic`.`id`, 
    COUNT('*') AS `fileCount`, 
    `Commit`.`id` AS `Commit.id`, 
    `SourceFile`.`id` AS `SourceFile.id` 
FROM 
    `commit_file_statistics` AS `CommitFileStatistic` 
LEFT OUTER JOIN `commits` AS `Commit` 
    ON `Commit`.`id` = `CommitFileStatistic`.`CommitId` 
LEFT OUTER JOIN `source_files` AS `SourceFile` 
    ON `SourceFile`.`id` = `CommitFileStatistic`.`SourceFileId` 
GROUP BY `SourceFileId` 
ORDER BY `fileCount` DESC;

这可能只适用于 sequelize 2.0 版 :)

This will probably only work on sequelize version 2.0 :)

这篇关于如何使用 Sequelize 跨 3 个表进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆