You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LEFT JOIN instead of INNER JOIN in findAndCountAll, count part #3936
(count part):
SELECT count(company.id) AS count FROM acrm_companies AS company INNER JOIN acrm_users AS user ON company.created_by = user.id INNER JOIN acrm_contacts AS contact ON company.company_id = contact.company_company_id AND contact.is_main_contact = true WHERE (company.deleted IS NULL AND (company.is_latest = true));
but, when company has no contact, than it will be not showed in result beacuse of INNER JOIN
but, i can't remove required: true / where: {..}, because when you search in datatables, query want to search in contact.name (it come as attribute from datatable) but it fail (because required: false don't include contact model)
So, solution can be LEFT JOIN, which include all companies and they have not to have contacts, something like this:
SELECT count(company.id) AS count FROM acrm_companies AS company INNER JOIN acrm_users AS user ON company.created_by = user.idLEFT JOIN acrm_contacts AS contact ON company.company_id = contact.company_company_id AND contact.is_main_contact = true WHERE (company.deleted IS NULL AND (company.is_latest = true));
required: true forces an inner join, use required: false to force a left join.
Reacted by iremkucukali, Chris H, Marcio Mansur, Patrick Kelly, Shaharyar Ahmed, Raphael D. Pinheiro, Stanislav Zavalishin, Phan Đức Việt, Max Truxa, Sergej Sha and 109 more
Reacted by MISSAOUI Aymen, Ilia Sidorenko, kyle, trichtrich, Iliya Repenko, Roman Galochkin, Osvi Ramos and Cyril CHAPON
Reacted by trichtrich, knnth, zhao5363, Saigal Amatya, Pavlo, CIZODEV, isaaxite, Roman Galochkin, Harman Manchanda and 温兴泵
Reacted by Max Mumford, Kuro, DevHarbor, caoyi, Ali Sherafat, trichtrich, Hayk Baghdasaryan, zhao5363, Saigal Amatya, Eugene C. and 9 more
Reacted by MISSAOUI Aymen, Ilia Sidorenko, trichtrich, Isaiah Taylor, Dmitry Nikitenko and Norrapat Nimmanee
Reacted by Phan Đức Việt, Toni Villena, Max Mumford, Kuro, todgru, MISSAOUI Aymen, trichtrich, knnth, Juan Trejo, Raghav Grover and 16 more
Reacted by kinnomick, timolecoola and Harman Manchanda
It is bugged - query is not builded same in count part as in data part,
i used required: false. It do LEFT OUTER JOIN in second (data query), but not that first (count) query. It completly ignores include it in count when required: false. Take a look:
1.query of method findAndCountAll()
SELECT count(company.id) AS count FROM acrm_companies AS company INNER JOIN acrm_users AS user ON company.created_by = user.id WHERE (company.deleted IS NULL AND (company.is_latest = true));
2.query of method findAndCountAll() (right after first)
SELECT company.id, company.company_id, company.name, company.name_alt, company.ico, contact.surname, company.ico, user.nickname, company.created, user.id AS user.id, user.nickname AS user.nickname, contact.id AS contact.id, contact.name AS contact.name, contact.surname AS contact.surname FROM acrm_companies AS company INNER JOIN acrm_users AS user ON company.created_by = user.id LEFT OUTER JOIN acrm_contacts AS contact ON company.company_id = contact.company_company_id AND contact.is_main_contact = true WHERE (company.deleted IS NULL AND (company.is_latest = true)) ORDER BY company.id ASC LIMIT 0, 50;
The bug appears to be that the JOIN probably shouldn't even be in the COUNT part since you can't use the where part to filter the main query if it's not an inner join.
Why should the include be kept if it is not required? The query will return both rows which have any contacts attached, and those which doesn't. The number of companies should still be the same, without and without the include part right. The only case is when required is true, and the join is limiting the number of company rows
But, imagine, that you have table on page and you search in table via API (ex. datatables) and you search in datatables via search input. On the API side, you have findAndCountAll. When you do search, than, you have to include model also in 'count' query, otherwise it will give you error of unknow attribute. Off course LEFT JOIN can be empty, but you have to join that table. And, off course, you dont want INNER JOIN if you expect that joining table can be empty (for ex.)
SELECTcount(*) AS`count`FROM`acrm_calls`AS`call`WHERE (`call`.`deleted` IS NULLAND (`call`.`id`=739164AND`user`.`nickname`LIKE'%david%'));
error:
Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'user.nickname' in 'field list'
so in this case, you have to do RIGHT JOIN in count (You can do INNER JOIN off course when you know that record must have user). But when joining model is not required, i have to do RIGHT JOIN
We don't officially support that syntax, so that's why you're seeing an error. We don't support adding a nested key to a toplevel attributes or where at the moment.
You can look at #3095 for a feature request for querying a include outside of include.where
I do see the problem here, even if we did officially support it, Sequelize doesn't know that you need the join inside the count.
This will produce an outer join. However, a where clause on a related model will create an inner join and return only the instances that have matching sub-models. To return all parent instances, you should add required: false.
SELECT `SiteMenu`.`id`, `placeholders`.`id`, count(`SiteMenu`.`id`) AS `count` FROM `site_menues` AS `SiteMenu` LEFT OUTER JOIN `menu_placeholders` AS `placeholders` ON `SiteMenu`.`id` = `placeholders`.`menuId` WHERE `SiteMenu`.`siteId` = 1 AND `SiteMenu`.`placeholders.id` IS NULL;
But i need placeholders.id` IS NULL` instead of SiteMenu.placeholders.id IS NULL
Pls help
Activity
mickhansen commentedon Jun 15, 2015
required: trueforces an inner join, userequired: falseto force a left join.Orvisky commentedon Jun 15, 2015
It is bugged - query is not builded same in count part as in data part,
i used
required: false. It do LEFT OUTER JOIN in second (data query), but not that first (count) query. It completly ignores include it in count whenrequired: false. Take a look:1.query of method findAndCountAll()
SELECT count(
company.id) AScountFROMacrm_companiesAScompanyINNER JOINacrm_usersASuserONcompany.created_by=user.idWHERE (company.deletedIS NULL AND (company.is_latest= true));2.query of method findAndCountAll() (right after first)
SELECT
company.id,company.company_id,company.name,company.name_alt,company.ico,contact.surname,company.ico,user.nickname,company.created,user.idASuser.id,user.nicknameASuser.nickname,contact.idAScontact.id,contact.nameAScontact.name,contact.surnameAScontact.surnameFROMacrm_companiesAScompanyINNER JOINacrm_usersASuserONcompany.created_by=user.idLEFT OUTER JOINacrm_contactsAScontactONcompany.company_id=contact.company_company_idANDcontact.is_main_contact= true WHERE (company.deletedIS NULL AND (company.is_latest= true)) ORDER BYcompany.idASC LIMIT 0, 50;[-]LEFT JOIN instead of INNER JOIN[/-][+]LEFT JOIN instead of INNER JOIN in findAndCountAll, count part[/+]Orvisky commentedon Jun 25, 2015
Anything new please about this problem?
mickhansen commentedon Jun 25, 2015
Haven't had time to look at it.
You can speed things along by providing a fully reproduceable example, even better a failing unit/integration test.
mickhansen commentedon Jun 25, 2015
The bug appears to be that the JOIN probably shouldn't even be in the COUNT part since you can't use the
wherepart to filter the main query if it's not an inner join.mickhansen commentedon Jun 25, 2015
Might be a bug here: https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1393 you can try playing around with that, see if it helps.
Orvisky commentedon Jun 25, 2015
Thx for fast reply :) If i make something, i will give a notice.
Orvisky commentedon Jun 25, 2015
Try look at line https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1397 , what do you think about changing to this:
return include.required || include.hasIncludeRequired || include.where;janmeier commentedon Jun 25, 2015
Why should the include be kept if it is not required? The query will return both rows which have any contacts attached, and those which doesn't. The number of companies should still be the same, without and without the include part right. The only case is when required is true, and the join is limiting the number of company rows
Orvisky commentedon Jun 25, 2015
But, imagine, that you have table on page and you search in table via API (ex. datatables) and you search in datatables via search input. On the API side, you have findAndCountAll. When you do search, than, you have to include model also in 'count' query, otherwise it will give you error of unknow attribute. Off course LEFT JOIN can be empty, but you have to join that table. And, off course, you dont want INNER JOIN if you expect that joining table can be empty (for ex.)
mickhansen commentedon Jun 25, 2015
@Orvacko If you need the join table in the count it has to be a inner join, else it doesn't really make sense.
Orvisky commentedon Jun 25, 2015
Simple example (actual models simplified) that throws you an error:
user). But when joining model is not required, i have to do RIGHT JOINmickhansen commentedon Jun 25, 2015
We don't officially support that syntax, so that's why you're seeing an error. We don't support adding a nested key to a toplevel attributes or where at the moment.
You can look at #3095 for a feature request for querying a include outside of
include.whereI do see the problem here, even if we did officially support it, Sequelize doesn't know that you need the join inside the count.
Orvisky commentedon Jun 25, 2015
Hm, and, what about that fix / feature i mentioned (or some other, better parameter) ? When i explicit say that include
Usermodel and it can do RIGHT JOIN, everything is Ok. I was expecting behavior like that is mention here: http://docs.sequelizejs.com/en/latest/docs/models-usage/#nested-eager-loading. WIth my fix, it is same behavior as in .doc , or not?mickhansen commentedon Jun 25, 2015
The behaviour described in the docs are for find* methods generally.
count has special logic so as not too muck up the query too much.
renatoargh commentedon Oct 4, 2015
Is there a way to force left joins in older versions like
"sequelize": "^1.7.10"? Thanks in advancejanmeier commentedon Oct 4, 2015
@renatoargh Unfortunately not
satornure commentedon Nov 24, 2015
I have the same problem, i tried
The result was
But i need
placeholders.id` IS NULL` instead ofSiteMenu.placeholders.idIS NULLPls help
janmeier commentedon Nov 24, 2015
Try
'$placeholders.id$': nullsatornure commentedon Nov 24, 2015
it works nice, thanks
janmeier commentedon Nov 24, 2015
Try upgrading to a more recent version
rcolmenarez commentedon Mar 2, 2016
Any advance in this bug?
nikuph commentedon Mar 18, 2016
This also helped me out for an
OUTER JOINwithWHERE.Is this somewhere in the documentation?