Repository navigation
Description
Hi all, I have a more complex model setup, and I stumbled across some weird behaviour when using and including relationships with a limit. Let's say I have this model setup, which is the same as https://sequelize.org/docs/v6/advanced-association-concepts/advanced-many-to-many/, but with an extra group (each profile should be part of a group).
const User = sequelize.define(
'user',
{
username: DataTypes.STRING,
}
)
const Profile = sequelize.define(
'profile',
{
name: DataTypes.STRING,
}
)
const Group = sequelize.define(
'group',
{
test: DataTypes.STRING,
}
)
const User_Profile = sequelize.define(
'User_Profile',
{
selfGranted: DataTypes.BOOLEAN,
},
{ timestamps: false }
)
// Many-to-many
User.belongsToMany(Profile, { through: User_Profile })
Profile.belongsToMany(User, { through: User_Profile })
// One-to-many
Group.hasMany(Profile)
Profile.belongsTo(Group)
Let's say I create only a single user, without any profile or group (this should be doable), using
await User.create({
username: 'Peter'
})
Now let's find all available users, but I do want to prefetch the group belonging to a profile (if the profile exists of course), using this code:
await User.findAll({
limit: 1,
include: [
{
model: Profile,
required: false,
include: [
{ model: Group, required: true }
]
},
],
})
Now the strange this is, this query doesn't yield any result, and the query looks like this:
SELECT `user`.*
,`profiles`.`id` AS `profiles.id`
,`profiles`.`name` AS `profiles.name`
,`profiles`.`createdAt` AS `profiles.createdAt`
,`profiles`.`updatedAt` AS `profiles.updatedAt`
,`profiles`.`groupId` AS `profiles.groupId`
,`profiles - > User_Profile`.`selfGranted` AS `profiles.User_Profile.selfGranted`
,`profiles - > User_Profile`.`userId` AS `profiles.User_Profile.userId`
,`profiles - > User_Profile`.`profileId` AS `profiles.User_Profile.profileId`
,`profiles - > group`.`id` AS `profiles.GROUP.id`
,`profiles - > group`.`test` AS `profiles.GROUP.test`
,`profiles - > group`.`createdAt` AS `profiles.GROUP.createdAt`
,`profiles - > group`.`updatedAt` AS `profiles.GROUP.updatedAt`
FROM (
SELECT `user`.`id`
,`user`.`username`
,`user`.`createdAt`
,`user`.`updatedAt`
FROM `users` AS `user`
WHERE (
SELECT `User_Profile`.`userId`
FROM `User_Profiles` AS `User_Profile`
INNER JOIN `profiles` AS `profile` ON `User_Profile`.`profileId` = `profile`.`id`
INNER JOIN `groups` AS `profile - > group` ON `profile`.`groupId` = `profile - > group`.`id`
WHERE (`user`.`id` = `User_Profile`.`userId`) LIMIT 1
) IS NOT NULL LIMIT 1
) AS `user`
LEFT OUTER JOIN (
(
`User_Profiles` AS `profiles - > User_Profile` INNER JOIN `profiles` AS `profiles` ON `profiles`.`id` = `profiles - > User_Profile`.`profileId`
) INNER JOIN `groups` AS `profiles - > group` ON `profiles`.`groupId` = `profiles - > group`.`id`
) ON `user`.`id` = `profiles - > User_Profile`.`userId`
If I remove the limit: 1 from the query, the actual user is returned with the following query:
SELECT `user`.`id`
,`user`.`username`
,`user`.`createdAt`
,`user`.`updatedAt`
,`profiles`.`id` AS `profiles.id`
,`profiles`.`name` AS `profiles.name`
,`profiles`.`createdAt` AS `profiles.createdAt`
,`profiles`.`updatedAt` AS `profiles.updatedAt`
,`profiles`.`groupId` AS `profiles.groupId`
,`profiles - > User_Profile`.`selfGranted` AS `profiles.User_Profile.selfGranted`
,`profiles - > User_Profile`.`userId` AS `profiles.User_Profile.userId`
,`profiles - > User_Profile`.`profileId` AS `profiles.User_Profile.profileId`
,`profiles - > group`.`id` AS `profiles.GROUP.id`
,`profiles - > group`.`test` AS `profiles.GROUP.test`
,`profiles - > group`.`createdAt` AS `profiles.GROUP.createdAt`
,`profiles - > group`.`updatedAt` AS `profiles.GROUP.updatedAt`
FROM `users` AS `user`
LEFT OUTER JOIN (
(
`User_Profiles` AS `profiles - > User_Profile` INNER JOIN `profiles` AS `profiles` ON `profiles`.`id` = `profiles - > User_Profile`.`profileId`
) INNER JOIN `groups` AS `profiles - > group` ON `profiles`.`groupId` = `profiles - > group`.`id`
) ON `user`.`id` = `profiles - > User_Profile`.`userId`
If I remove the inclusion of the group, the user is properly returned with the query
SELECT `user`.*
,`profiles`.`id` AS `profiles.id`
,`profiles`.`name` AS `profiles.name`
,`profiles`.`createdAt` AS `profiles.createdAt`
,`profiles`.`updatedAt` AS `profiles.updatedAt`
,`profiles`.`groupId` AS `profiles.groupId`
,`profiles - > User_Profile`.`createdAt` AS `profiles.User_Profile.createdAt`
,`profiles - > User_Profile`.`updatedAt` AS `profiles.User_Profile.updatedAt`
,`profiles - > User_Profile`.`userId` AS `profiles.User_Profile.userId`
,`profiles - > User_Profile`.`profileId` AS `profiles.User_Profile.profileId`
FROM (
SELECT `user`.`id`
,`user`.`username`
,`user`.`createdAt`
,`user`.`updatedAt`
FROM `users` AS `user` LIMIT 1
) AS `user`
LEFT OUTER JOIN (
`User_Profile` AS `profiles - > User_Profile` INNER JOIN `profiles` AS `profiles` ON `profiles`.`id` = `profiles - > User_Profile`.`profileId`
) ON `user`.`id` = `profiles - > User_Profile`.`userId`
If I don't force the group to be included, i.e. remove the required: true (it could be possible, since I can set the foreign key to be not nullable), the user is returned with the following query:
SELECT `user`.*
,`profiles`.`id` AS `profiles.id`
,`profiles`.`name` AS `profiles.name`
,`profiles`.`createdAt` AS `profiles.createdAt`
,`profiles`.`updatedAt` AS `profiles.updatedAt`
,`profiles`.`groupId` AS `profiles.groupId`
,`profiles - > User_Profile`.`createdAt` AS `profiles.User_Profile.createdAt`
,`profiles - > User_Profile`.`updatedAt` AS `profiles.User_Profile.updatedAt`
,`profiles - > User_Profile`.`userId` AS `profiles.User_Profile.userId`
,`profiles - > User_Profile`.`profileId` AS `profiles.User_Profile.profileId`
,`profiles - > group`.`id` AS `profiles.GROUP.id`
,`profiles - > group`.`test` AS `profiles.GROUP.test`
,`profiles - > group`.`createdAt` AS `profiles.GROUP.createdAt`
,`profiles - > group`.`updatedAt` AS `profiles.GROUP.updatedAt`
FROM (
SELECT `user`.`id`
,`user`.`username`
,`user`.`createdAt`
,`user`.`updatedAt`
FROM `users` AS `user` LIMIT 1
) AS `user`
LEFT OUTER JOIN (
`User_Profile` AS `profiles - > User_Profile` INNER JOIN `profiles` AS `profiles` ON `profiles`.`id` = `profiles - > User_Profile`.`profileId`
) ON `user`.`id` = `profiles - > User_Profile`.`userId`
LEFT OUTER JOIN `groups` AS `profiles - > group` ON `profiles`.`groupId` = `profiles - > group`.`id`
Since it's a many-to-many relationship, I can't use separate: true. Nor can I set a limit: 1000000 somewhere at the profile or group, as that gives me the error that only HasMany relationships support separate: true. Is this a bug, or am I using it wrong?
Sequelize v6.37.7
UPDATE
It seems that when using subQuery: false, the user is actually returned, even with the limit included. Not sure why that works, but apparently it does... Would this be the solution?
Activity
tusharrana786 commentedon Jun 8, 2025
@PeterTillema
Well i will try to go step by step of what i understand
why does'nt it work with limit:1
The reason for this is in your query you are askinig for users that have profiles to must have groups, now when you perform a limit:1 sequelize tries to ensure the same profile requirement is satisfied, that's why it performs an inner join in where clause to prevent a scenario where there might be a user with profile and group but another user with no profile is returned first due to limit one, so basically it's the combination of limit:1 and nested required: true that is causing this behaviour.
why it works when you use subquery: false
So basically when you use subquery: false any sort of limiting is apllied after all the joins are made so in ur case since there is only one user with profile and group it actually returns the user but if there were 2 users one same as current one and other with profile and group this query might return user without profile thus breaking the group for profile required constraint, subquery: true(default behaviour) will work better in this case as it will return the user with profile.