Repository navigation
Description
In my domain model I have the following three models:
var challenge = sequelize.define('Challenge', {
challengeId: {
field: 'challenge_id',
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
unique: true
},
ownerId: {
type: DataTypes.INTEGER,
references: { model: 'USER', key: 'user_id'},
allowNull: false
},
description: {
field: 'description',
type: DataTypes.TEXT,
allowNull: false
}
};
challenge.belongsTo(models.User, {foreignKey: 'ownerId'});
challenge.hasMany(models.Idea, {foreignKey: 'challengeId'});
var idea = sequelize.define('Idea', {
ideaId: {
field: 'idea_id',
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
unique: true
},
challengeId: {
field: 'challenge_id',
type: DataTypes.INTEGER,
references: { model: 'CHALLENGE', key: 'challenge_id'},
allowNull: true
},
description: {
field: 'description',
type: DataTypes.STRING(600),
allowNull: false
}
};
idea.belongsTo(models.Challenge, {foreignKey: 'challengeId'});
var user = sequelize.define('User', {
userId: {
field: 'user_id',
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
unique: true
},
name: {
field: 'name',
type: DataTypes.STRING,
allowNull: false
}
}
user.hasMany(models.Challenge, {foreignKey: 'ownerId'});This represents a simple domain model where a challenge is owned by a specific user. An idea is linked to one or zero challenges.
Now I would like to search for all challenges and also add a count of how many ideas are associated with that challenge. This query is something i got working, but as soon as i also want to include userinformation, the joins in the generated SQL are not correct.
To query the challenges and get a count of ideas I do this:
models.Challenge.findAll({
attributes: ["Challenge.*", [models.sequelize.fn('COUNT', models.sequelize.col('Ideas.idea_id')), 'IdeaCount']],
include: [{
model: models.Idea,
attributes: []
}],
group: ["Challenge.challenge_id"],
order: [['count', 'ASC']]
}).then(function (rows) {
console.log(rows)
});Which executes the correct SQL query:
SELECT "Challenge"."challenge_id" AS "challengeId",
"Challenge".*,
COUNT("Ideas"."idea_id") AS "IdeaCount"
FROM "CHALLENGE" AS "Challenge"
LEFT OUTER JOIN "IDEA" AS "Ideas"
ON "Challenge"."challenge_id" = "Ideas"."challenge_id"
WHERE "Challenge"."company_id" = 1
GROUP BY "Challenge"."challenge_id"
ORDER BY "createdAt" DESC; Now when I want to also include information from the creator of the challenges, i get the wrong SQL.
models.Challenge.findAll({
attributes: ["Challenge.*", [models.sequelize.fn('COUNT', models.sequelize.col('Ideas.idea_id')), 'IdeaCount']],
include: [{
model: models.Idea,
attributes: []
},
{
model: models.User,
attributes: ["userId", "name"]
}],
group: ["Challenge.challenge_id"],
order: [['count', 'ASC']]
}).then(function (rows) {
console.log(rows)
});Which results in the following SQL:
SELECT "Challenge".*,
COUNT("Ideas"."idea_id") AS "IdeaCount",
"User".*
FROM "CHALLENGE" AS "Challenge"
LEFT OUTER JOIN "IDEA" AS "Ideas"
ON "Challenge"."challenge_id" = "Ideas"."challenge_id"
INNER JOIN "USER" AS "User"
ON "Challenge"."ownerId" = "User"."user_id"
LEFT OUTER JOIN ("USER_ROLE" AS "User.Roles.USER_ROLE"
INNER JOIN "ROLE" AS "User.Roles"
ON "User.Roles"."role_id" =
"User.Roles.USER_ROLE"."feature_id")
ON "User"."user_id" = "User.Roles.USER_ROLE"."user_id"
WHERE "Challenge"."company_id" = 1
GROUP BY "Challenge"."challenge_id"
LIMIT 20;Instead I would need an SQL query like this:
SELECT "Challenge".*,
"User"."user_id",
"User"."firstname",
"User"."lastname"
FROM (SELECT "Challenge".*,
COUNT("Ideas"."idea_id") AS "IdeaCount"
FROM "CHALLENGE" AS "Challenge"
LEFT OUTER JOIN "IDEA" AS "Ideas"
ON "Challenge"."challenge_id" =
"Ideas"."challenge_id"
WHERE "Challenge"."company_id" = 1
GROUP BY "Challenge"."challenge_id") AS "Challenge"
LEFT OUTER JOIN "USER" AS "User"
ON "User"."user_id" = "Challenge"."ownerId"
ORDER BY "createdAt" DESC
LIMIT 20; It seems like sequelize puts the left outer join for the user on the same level as the one that is used for counting the assocations. Instead, the join for the user information should happen outside of it. Is it possible to influence sequelize in such a way that this joins is done separately like is done as in the SQL statement described above?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
stale commentedon Jan 1, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
popra commentedon Aug 17, 2018
This is still an issue
MaartenGe commentedon Feb 5, 2019
I'm currently having the same issue where I'd like to count 2 many to many associations and no multiple includes are possible
davehowson commentedon Dec 31, 2019
Still an issue
deleteeeeeeeeeeeed commentedon Feb 27, 2020
still issueeeee
i can not count it
dobrovolskaa commentedon Apr 5, 2021
still an issue :c
karimmakram commentedon Jun 18, 2022
still an issue
but you can solve count with DISTINCT
[models.sequelize.fn('COUNT',models.sequelize.fn('DISTINCT' models.sequelize.col('Ideas.idea_id'))), 'IdeaCount']
but other operation like sum still duplicate data
abdukulov-talgat commentedon Oct 30, 2022
Same issue here.