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
Reacted by Andrew Aarestad, Anthony, Ian Lovett, Janek Thomaschewski, Илия, Brian Schardt, Alexander, Chris Fremgen, Kees Kluskens, Ryo Maeda and 48 more
Reacted by Anik Hasan, Ryota Bannai, Jared Silver, Jeffyter Saraiva, Julian David and André Thiele
I guess you're looking for relations in find options parameters.
Reacted by Carlos Kassab, Mitchell Sullivan, Diego Julião, Jacob Martin, jiridudekusy, Diogo Cardoso, AnthonyDreams, Sufiane, Hwanseok Kang (Barth), Luís Mazoni and 8 more
Same issue here. Anyone knows how to perform such a find?
To give a better example
Car { id, user }
User { id, company }
Company { id }
// I want to find cars with specific company ID
// something like this, but it's not working
let carsOfCompany = await getRepository(Car).find({
relations: ['user'],
where: {
user: {
companyId: 'someid'
}
}
})
The relation where only seems to work on id or perhaps primary columns,
EG if your Car has user with JoinColumn() you could successfully query on where: { user: { id: 123 } } however it doesnt seem to work for non primary columns or joins such as the user's companyId
Reacted by Daryl, Jonathan D'Orleans, Thomas Zwarts, M. Ishara Madawa Kumararathna, Danny França, Hasil T, Mitchell Sullivan, Pierre Voisin, Carlos Daniel, Güneş and 17 more
Reacted by M. Ishara Madawa Kumararathna, Güneş, Hara Kang, Sufiane and Hwanseok Kang (Barth)
I found it unexpected that the orm was not able to consider that relational query
Reacted by Jonathan D'Orleans, Thomas Zwarts, Jakub Kopańko, Mitchell Sullivan, Willson Fernandes, Sebastian, jose-bt, Klaas Foppen, Jalil Arfaoui, Denis and 8 more
There's a workaround for filtering based on relation fields for findOne()/find() methods that I've discovered recently. The problem with filtering related table fields only exists for ObjectLiteral-style where, while string conditions work perfectly.
Assume that we have two entities – User and Role, user belongs to one role, role has many users:
Now we can call findOne()/find() methods of EntityManager or repository:
roleRepository.find({join: {alias: 'roles',innerJoin: {users: 'roles.users'}},where: qb=>{qb.where({// Filter Role fieldsa: 1,b: 2}).andWhere('users.name = :userName',{userName: 'John Doe'});// Filter related field}});
You can omit the join part if you've marked your relation as an eager one.
Reacted by Islam Salem, Janek Thomaschewski, BenLee, Mateo Karadža, MrP, Ben Stickley, Michał Kolbusz, Thomas Zwarts, Todd Brannam, Sheik Althaf and 91 more
Reacted by Josue Quinteros, Pierre-Alexandre and Amr ahmed
Reacted by Islam Salem, Thomas Zwarts, Sheik Althaf, Derek Zhou, Prateek Kathal, Davran Sabiraliev, Carlos Kassab, Juan Jose Lopez Martinez, Stephanie Ricardo, Morita Naoki and 18 more
Reacted by arturz, Islam Salem, Michał Kolbusz, Thomas Zwarts, Sheik Althaf, Enes, Prateek Kathal, Laure Retru-Chavastel, Aakash Raj, 0xFrizen.eth and 26 more
Reacted by edu, John Amorim, Andrii Varlamov, Sean, Nasir Khan, Shihab Mridha, Jatin Katyal, Karan Raina, Andrii Ponamarov(Dresha), mztnnrt and 4 more
Reacted by lamhungypl, Clément Haller, Karim, mztnnrt and Josue Quinteros
The relation where only seems to work on id or perhaps primary columns,
EG if your Car has user with JoinColumn() you could successfully query on where: { user: { id: 123 } } however it doesnt seem to work for non primary columns or joins such as the user's companyId
Honestly, this should stated in docs somewhere - saved me hours!
Thank you
Reacted by Jonathan D'Orleans, Thomas Zwarts, Brian Schardt, Thomas Gauvin, Carlos Daniel, Jean-Guillaume Buret, Sebastian, Jhon Zambrano, Francesco Belvedere, Sufiane and 7 more
I found the same issue, but for postgres...
Seems like the find method signatures could be more strict, so that all the different variants don't get mingled without clear error handling
The relation where only seems to work on id or perhaps primary columns,
EG if your Car has user with JoinColumn() you could successfully query on where: { user: { id: 123 } } however it doesn't seem to work for non primary columns or joins such as the user's companyId
How sad! I just confirmed that too (I'm not using annotations but the results are the same). Searching with something like the following doesn't work whilst using "branchId" (primary key) instead of "languageId" does work:
where: {
name: {
languageId: 7,
},
}
@niccolofanton - thanks for the snippet! It does the trick but, to me, having to rely on somewhat raw SQL defeats the ORM purpose a little...
Reacted by Jean-Guillaume Buret, Nahuel Lucero, Willson Fernandes, Alexandru Căncescu, mickaelmesdocteurs, Francesco Belvedere, Marek Lukáš, Davron Sherbaev, Nelson Ononivu, Niccolò Fanton and 1 more
has finding an entity with relation condition been implemented?
I think this is a necessity for an ORM
Reacted by Nelson Ononivu, ddrenteria, Henrique Leite, Enzo Arauco, Martin Horváth, Yusuf Adeniyi, Bruno de Rezende, Roman, Sebők Bence, Martin Sotirov and 3 more
Hi, is anyone working on this? I can take this issue and try to send a pull request.
Reacted by Amin Rasouli, Eduard Rykhtiuk, Vu Le, derSoerrn95, Matija Kop and Juan Estrada
Reacted by Amin Rasouli and Eduard Rykhtiuk
Reacted by Amin Rasouli, Eduard Rykhtiuk and derSoerrn95
Reacted by Felix Gohla, David Jandrey, Wojtek, Minh Le, Muhammad Alfian Rasyidin, Yudi Hertanto, Shinse Tanaka, gkTim, K Turnbull, Sebők Bence and 18 more
Reacted by Amin Rasouli, Eduard Rykhtiuk, Vu Le and derSoerrn95
Reacted by Amin Rasouli, Eduard Rykhtiuk and Brendenson Andrade
Activity
Kononnable commentedon Aug 31, 2019
I guess you're looking for
relationsin find options parameters.anthonyjlmorel commentedon Sep 10, 2019
Hi guys, what's the status on this ?
I would like to be able to request entities placing where condition on joined relations too which seems either missing or undocumented.
Could you advise on this ?
march08 commentedon Sep 26, 2019
Same issue here. Anyone knows how to perform such a find?
To give a better example
JunquerGJ commentedon Oct 29, 2019
@march08
I cannot check it right now, but maybe this works for you:
let carsOfCompany = await getRepository(Car).find({ join: { alias: "car", leftJoinAndSelect: { company: "car.company" } }, where : { company : { id : 'someid'}} })ilovett commentedon Dec 4, 2019
The relation where only seems to work on
idor perhaps primary columns,EG if your Car has
userwithJoinColumn()you could successfully query onwhere: { user: { id: 123 } }however it doesnt seem to work for non primary columns or joins such as the user'scompanyIdilovett commentedon Dec 4, 2019
I found it unexpected that the orm was not able to consider that relational query
ilovett commentedon Dec 4, 2019
In my query I tried to
findwhere{ user: { mongoId: "xxx" } }but myThing.find({ where: { user: { mongoId: "xxx" } })yielded:I am guessing because my
Thing.user@JoinColumnisidand notmongoIdalpharder commentedon Dec 16, 2019
There's a workaround for filtering based on relation fields for
findOne()/find()methods that I've discovered recently. The problem with filtering related table fields only exists forObjectLiteral-stylewhere, while string conditions work perfectly.Assume that we have two entities –
UserandRole, user belongs to one role, role has many users:Now we can call
findOne()/find()methods ofEntityManageror repository:You can omit the
joinpart if you've marked your relation as an eager one.darylcecile commentedon Mar 22, 2020
Honestly, this should stated in docs somewhere - saved me hours!
Thank you
kop7 commentedon Apr 16, 2020
find cars where model location is 2
but miss locationId parameter?...
what am I doing wrong?
fchu commentedon Apr 24, 2020
I found the same issue, but for postgres...
Seems like the find method signatures could be more strict, so that all the different variants don't get mingled without clear error handling
david-macharia commentedon May 6, 2020
@COP
Did you find out why where is ignored?
grigdevelop commentedon May 17, 2020
You can also get entities with all relations.
2 remaining items
pvoisin commentedon Aug 26, 2020
How sad! I just confirmed that too (I'm not using annotations but the results are the same). Searching with something like the following doesn't work whilst using "branchId" (primary key) instead of "languageId" does work:
@niccolofanton - thanks for the snippet! It does the trick but, to me, having to rely on somewhat raw SQL defeats the ORM purpose a little...
BenSjoberg commentedon Nov 2, 2020
Seems related to #2707
nelsonsvo commentedon Apr 21, 2021
has finding an entity with relation condition been implemented?
I think this is a necessity for an ORM
pradeep0601 commentedon Apr 26, 2021
Eagerly waiting for this feature to search based on relation fields other than the primary key.
jeffersonlicet commentedon May 15, 2021
Hi, is anyone working on this? I can take this issue and try to send a pull request.
MardariG commentedon Jun 14, 2021
any status here?
jeffersonlicet commentedon Jun 14, 2021
By default only JoinColumns are used (id) I'm trying to include relation cols in a non-intrusive way.
Goldziher commentedon Jun 17, 2021
wow this is a bad bug
jeffersonlicet commentedon Jun 17, 2021
I'll adventure myself and send a non-optimized pull request so I can get feedback about a better way to do this. But seems like it's working now.
salvationarinze commentedon Jun 23, 2021
Any solution to this yet?
imnotjames commentedon Jun 23, 2021
This is directly related to #2707. If that gets fixed, this does.