Repository navigation
Closed
Labels
Description
Curious if the implementation has changed on how to query on associations. I created a test case that used to work on v2.0.0-rc8 but is currently not working on v2.0.0. Was hoping for some clarity on how to adjust my code for the new changes. Thanks for assistance.
v2.0.0-rc8 query:
`Company`.`id`,
`Company`.`name`,
`Company`.`createdAt`,
`Company`.`updatedAt`,
`Company`.`presidentId`,
`Company`.`departmentId`,
`President`.`id` AS `President.id`,
`President`.`name` AS `President.name`,
`President`.`createdAt` AS `President.createdAt`,
`President`.`updatedAt` AS `President.updatedAt`,
`Department`.`id` AS `Department.id`,
`Department`.`name` AS `Department.name`,
`Department`.`createdAt` AS `Department.createdAt`,
`Department`.`updatedAt` AS `Department.updatedAt`,
`Department`.`leadId` AS `Department.leadId`,
`Department.Lead`.`id` AS `Department.Lead.id`,
`Department.Lead`.`name` AS `Department.Lead.name`,
`Department.Lead`.`createdAt` AS `Department.Lead.createdAt`,
`Department.Lead`.`updatedAt` AS `Department.Lead.updatedAt`
FROM
`Companies` AS `Company`
LEFT OUTER JOIN
`Users` AS `President` ON `Company`.`presidentId` = `President`.`id`
LEFT OUTER JOIN
`Departments` AS `Department` ON `Company`.`departmentId` = `Department`.`id`
LEFT OUTER JOIN
`Users` AS `Department.Lead` ON `Department`.`leadId` = `Department.Lead`.`id`
WHERE
(`President`.`name` = 'OurLead'
OR `Department.Lead`.`name` = 'OurLead');
v2.0.0 query:
`Company`.`id`,
`Company`.`name`,
`Company`.`createdAt`,
`Company`.`updatedAt`,
`Company`.`presidentId`,
`Company`.`departmentId`,
`President`.`id` AS `President.id`,
`President`.`name` AS `President.name`,
`President`.`createdAt` AS `President.createdAt`,
`President`.`updatedAt` AS `President.updatedAt`,
`Department`.`id` AS `Department.id`,
`Department`.`name` AS `Department.name`,
`Department`.`createdAt` AS `Department.createdAt`,
`Department`.`updatedAt` AS `Department.updatedAt`,
`Department`.`leadId` AS `Department.leadId`,
`Department.Lead`.`id` AS `Department.Lead.id`,
`Department.Lead`.`name` AS `Department.Lead.name`,
`Department.Lead`.`createdAt` AS `Department.Lead.createdAt`,
`Department.Lead`.`updatedAt` AS `Department.Lead.updatedAt`
FROM
`Companies` AS `Company`
LEFT OUTER JOIN
`Users` AS `President` ON `Company`.`presidentId` = `President`.`id`
LEFT OUTER JOIN
`Departments` AS `Department` ON `Company`.`departmentId` = `Department`.`id`
LEFT OUTER JOIN
`Users` AS `Department.Lead` ON `Department`.`leadId` = `Department.Lead`.`id`
WHERE
(`Company`.`President.name` = 'OurLead'
OR `Company`.`Department.Lead.name` = 'OurLead');
Reacted by Wesley Walser, Thomas Louvigné, Joseph Ashwin Kottapurath and Gabriel Shomorin
Activity
mickhansen commentedon Feb 10, 2015
Pretty sure that where with nested keys didn't work in rc8 either.
But yeah nested keys are insanely error prone so we don't really support them currently.
mickhansen commentedon Feb 10, 2015
We need some new syntax for defining include based where's that don't go on a join (for
OR) mostly.DavidTPate commentedon Feb 11, 2015
I don't think it is the right long term solution, but I think this could be accomplished in the short term (until a better solution is implemented) using a literal. If you know the table name, as we do in that test case I think we could just do:
instead of
mickhansen commentedon Feb 11, 2015
@DavidTPate Unfortuneatly
literal()can't be used as a key.I'm very open to general API discussion for this. But i don't think relying on nested keys is the way to go.
toaster33 commentedon Feb 12, 2015
I think I was able to narrow down the cause...
\abstract\query-generator.js
v2.0.0
v2.0.0-rc8
toaster33 commentedon Feb 13, 2015
To get conversations / ideas rolling...
First I was thinking another attribute within the include but cannot figure out how the attribute would be combined with other attributes. If the following existed, I still would not know if the attributes should be ANDed or ORed.
Building out the where using includes but this seems too cumbersome and redundant:
Another variation of what worked previously (please refer to test link line 80):
mickhansen commentedon Feb 13, 2015
Unfortuneatly it's not possible to have references as keys (atleast in ES5).
The problem with nested keys is that it was error prone, but perhaps we could use a special syntax to denote that it's an include:
{'$Company.Department.key$': value}.That still poses a general issue though, we took out include on string because it was very error prone and not very deterministic.
mickhansen commentedon Feb 13, 2015
Since it's possible to save association references, perhaps something like:
toaster33 commentedon Feb 14, 2015
I like your last suggestion which would not incur much of a change (at least for me).
sebay00 commentedon Feb 25, 2015
Hi there,
Just want to know if you decided how to fix this issue / improvement ?
mickhansen commentedon Feb 25, 2015
@sebay00 not finally, but likely the last proposal i commenetd.
DavidTPate commentedon Feb 25, 2015
@mickhansen I think you last suggestion of adding
$associationwould work well.36 remaining items
ConAntonakos commentedon Jun 21, 2017
This issue thread really helped me find out how to use the query association syntax in the where clause from #3095 (comment):
$table_name.field_name$: field_valueI didn't see this in the docs. Is it there and I just missed it, or do we need a PR? 😄
Edit: I see it now, but it was not easily understood at first.
DavidTPate commentedon Jun 22, 2017
@ConAntonakos if you don't see it in the docs, I'd suggest putting in a PR.
webdeveloper0429 commentedon Dec 1, 2017
here is my issue.
when include is duplicated, error shows
if I didn't include rightList, then this query works well.
but if I use sub include, error shows "SequelizeDatabaseError"
aprilmintacpineda commentedon Feb 23, 2018
if you want to query base on the association, do it like this.
sequelize@^4.33.4testfile.js
query generated
output
mdemblani commentedon Jan 13, 2019
This method works as long as there is a single mode in the join. It fails the moment when there is another model with a different type of relation, i.e. belongsToMany
smk-data commentedon Apr 21, 2019
Table0->Table1->Table2
field_name: Sequelize.where(Sequelize.col('Table1->Table2.field_name'), {
[Op.like]: '%field_name'%'
}),
AshBringer4eg commentedon Feb 10, 2020
You can use something like this:
AshBringer4eg commentedon Feb 11, 2020
Or another solution. it may replace Task where case in one row with JOIN
fabioespinosa commentedon Mar 5, 2020
Is it possible to reference a table querying JSONB and the
$col$operator? like:Gives me an error.
vinrsr commentedon Dec 17, 2020
Does anyone realized that i found something that is working for this kind of problem. I've been suffering for 3 days and finally i found it out.
AshBringer4eg commentedon Dec 24, 2020
Try something like this:
vosho commentedon Mar 9, 2021
The first solution has one problem:
When in findcountall, the returned rows are not as expected when limit is used.
The second solution is different from the original question
The condition is or, but your reply becomes and between query and sub queries
geeofree commentedon Feb 25, 2022
#3095 (comment)
Thank you! This worked for my situation. Can't believe this isn't documented however.