Sequelize group by include models work correct in MySQL, but not in SQL Server, core codes are as following
Models
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:[] }]
})
Above code runs well in mySQL, but when runs in SQL Server, I get error "Column 'topics.id' is invalid in the selected list...."