Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Sequelize group by include模型在MySQL中工作正常,但得到;专栏。。。“在选择列表中无效…”;SQL Server中出现错误_Node.js_Sql Server_Sequelize.js - Fatal编程技术网

Node.js Sequelize group by include模型在MySQL中工作正常,但得到;专栏。。。“在选择列表中无效…”;SQL Server中出现错误

Node.js Sequelize group by include模型在MySQL中工作正常,但得到;专栏。。。“在选择列表中无效…”;SQL Server中出现错误,node.js,sql-server,sequelize.js,Node.js,Sql Server,Sequelize.js,Sequelize group by include模型在MySQL中工作正常,但在SQL Server中工作不正常,核心代码如下 模型 var topics = sequelize.define('topics', { id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false, autoIncrement: t

Sequelize group by include模型在MySQL中工作正常,但在SQL Server中工作不正常,核心代码如下

模型

var topics = sequelize.define('topics', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            autoIncrement: true
        },
        title:{
            type:  DataTypes.STRING,
            allowNull: false
        }
});
var comment = sequelize.define('comment', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            allowNull: false,
            autoIncrement: true
        },
        text: { type:DataTypes.TEXT, allowNull:false },
        topic_id: { type: DataTypes.INTEGER, allowNull:false }
});

// Association
comment.hasOne(topics, {foreignKey:"id", sourceKey:"topic_id"})
topics.hasMany(comment, { foreignKey:"topic_id", sourceKey:"id" })

// Query get error in SQL Server, but goes well in MySQL
topics.findAll({
        group:["topics.id"],
        include: [{ model:comment, attributes:[] }]
})

上述代码在mySQL中运行良好,但在SQL Server中运行时,我得到错误“Column'topics.id'在所选列表中无效…”“

我已经解决了这个问题,在MSSQL中,诀窍是您需要列出组中的所有字段,如下所示:

topics.findAll({
   group:["topics.id","topics.title"], // <---- need to list "topics.title"
   include: [{ model:comment, attributes:[] }],
   attributes:{
      include:[[sequelize.fn('count',sequelize.col('comments.id')),'participants']]
   }
})
topics.findAll({

组:[“topics.id”,“topics.title”],//@RohitDalal,谢谢你的回复,但我很少了解SQL,我的问题是查询无法在SQL中操作组操作,如下所示:topics.findAll({group:[“topics.id”],include:[{model:comment,attributes:[]}]),如果我删除group by,一切都会顺利