So this is really annoying me and causing my system a lot of problems.
Say you have the following include:
include: [
{model: model.profile},
{model: model.company_profile},
{model: model.title},
{
model: model.division,
include: [{model: model.location}],
},
{model: model.user_type},
{model: model.phone},
{
model: model.user_has_user_type,
include:{
model:model.user_type
}
}
],
First you add the where statement:
query.where = {
organization_id: orgId, division_id: {$in: adminDivisions},
}
You then add the following order by:
query.order = [
[model.division, 'name','DESC' ]
];
And you attempt to call a user table:
model.user.fetchAll(query);
Now this works HOWEVER something is very wrong with the data output:
if you check the query that it runs you will find something like this:
FROM (SELECT
`user`.`id`,
`user`.`username`,
`user`.`is_active`,
`user`.`user_type_id`,
`user`.`organization_id`,
`user`.`title_id`,
`user`.`image_path`,
`user`.`division_id`,
`user`.`timestamp`,
`user`.`can_buy`,
`user`.`activation_email_sent`
FROM `user` AS `user`
WHERE
`user`.`organization_id` = 12 AND `user`.`division_id` IN (64, 75, 76, 77, 133, 179) AND `user`.`is_active` = 1
LIMIT 10)
After this sub query there is all of the JOINS. and at the VERY end :
ORDER BY `division`.`name` DESC;
This renders the ORDER BY statement completely useless because the 10 users (which the query is limited to) is already selected. So basically you are just ordering your already picked output and not the whole table.
My question is how can i avoid this? how can i make sure that it orders correctly? is this a flaw in sequelize? or am i doing anything wrong?
via Marc Rasmussen
No comments:
Post a Comment