Repository navigation
Description
Hello,
First thank you for your module, which has been instrumental in developing my current project.
What are you doing?
I've been trying to use a top level where with eager loaded models, using as a reference this part of the documentation : http://docs.sequelizejs.com/manual/tutorial/models-usage.html#top-level-where-with-eagerly-loaded-models.
My data model is the following (detail of my models under the request) :
A user has one main email (user.email), one professional email (user.professional_email), and various other user emails from a table (user_emails).
I'm trying to find a user who would have either his primary email (user.email) his professional email (user.professional_email) or any email from user_emails table equal one email address.
My request with eager loaded models :
return models.Users.findOne({
where: {
[Op.or]: [
{ email: < email > },
{ professional_email: < email > },
{ '$user_emails.email$': < email > },
],
},
attributes: ['id', 'email', 'professional_email'],
include: [{
as: 'user_emails',
model: models.UserEmails,
attributes: ['id', 'user_id', 'email'],
}],
});For more details here are my models :
Model for my user table :
const User = sequelize.define('users', {
id: { ... },
email: {
type: DataTypes.STRING,
unique: true,
validate: {
isEmail: true,
notEmpty: true,
},
},
professional_email: {
type: DataTypes.STRING,
unique: true,
validate: {
isEmail: true,
},
},
... // Some other properties
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
timestamps: true,
underscored: true,
paranoid: true,
});
Users.associate = function associate(models) {
Users.hasMany(models.UserEmails);
};Model for my user_emails table :
const User = sequelize.define('user_emails', {
id: { ... },
email: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
charset: 'utf8',
collate: 'utf8_unicode_ci',
underscored: true,
timestamps: true,
updatedAt: false,
});
UserEmails.associate = function associate(models) {
UserEmails.belongsTo(models.Users);
};What do you expect to happen?
I expected this request to return me one user that would have as user.email, user.professional_email or any user_email.email the value of < email >.
I expected Sequelize to create the following SQL request which solves my problem :
SELECT `users`.*,`user_emails`.`id` AS `user_emails.id`, `user_emails`.`user_id` AS `user_emails.user_id`, `user_emails`.`email` AS `user_emails.email`
FROM `users` AS `users`
LEFT OUTER JOIN `user_emails` AS `user_emails` ON `users`.`id` = `user_emails`.`user_id`
WHERE (`users`.`deleted_at` > '2018-02-23 14:27:31' OR `users`.`deleted_at` IS NULL)
AND (`users`.`email` = < email > OR `users`.`professional_email` = < email > OR `user_emails`.`email` = < email >)
LIMIT 1;
What is actually happening?
I got the following error : SequelizeDatabaseError: Unknown column 'user_emails.email' in 'where clause'.
Sequelize creates the following SQL request which fails :
SELECT `users`.*, `user_emails`.`id` AS `user_emails.id`, `user_emails`.`user_id` AS `user_emails.user_id`, `user_emails`.`email` AS `user_emails.email`
FROM (
SELECT `users`.`id`, `users`.`email`, `users`.`professional_email`
FROM `users` AS `users` WHERE ((`users`.`deleted_at` > '2018-02-23 14:27:31' OR `users`.`deleted_at` IS NULL) AND (`users`.`email` = < email > OR `users`.`professional_email` = < email > OR `user_emails`.`email` = < email >))
LIMIT 1)
AS `users`
LEFT OUTER JOIN `user_emails` AS `user_emails` ON `users`.`id` = `user_emails`.`user_id`;
It seems that the where is nested inside a second select.
I'm not entirely sur that I'm not doing something wrong, and the answer is obvious 🤔
Thank you for your help !
Dialect: mysql
Dialect version: 14.14
Database version: 5.7.14
Sequelize version: 4.33.4
Tested with latest release: Yes
Activity
aprilmintacpineda commentedon Feb 25, 2018
this could be related to #9095
EdouardDubois commentedon Feb 26, 2018
@aprilmintacpineda Yes, I've tried with findAll and the query is the correct one, so this is a bug with the LIMIT included in the findOne.
I'm going to go with findAll for now since I was only checking for existence.
Thanks a lot !
EdouardDubois commentedon Mar 29, 2018
Hello,
Did you get the time to get a look at this issue ?
Sadly this issue is limiting performances overall on my side when I want to implement pagination on several otherwise huge lists.
Thank you,
sushantdhiman commentedon Apr 7, 2018
There are many issues reported for this same bug, like others discussion moved to #9166