-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Is it possible to include Nth-level relationship data using find options? #1270
Comments
You can create your query by using the query builder let personsWithHomesAndTypes = await repository.createQueryBuilder("person")
.innerJoinAndSelect("person.homes", "home")
.innerJoinAndSelect("homes.homeType", "homeType")
.getMany() |
Since you are suggesting using a different method, is it safe to assume that the answer to my question is "no", that I cannot accomplish what I am trying to do using the find options? |
@daniel-lang your comment combine with a more thorough reading of the doc has provided me with the answer to my question. It is in fact possible using the find options, I just needed to use the "join" option instead of the relations.
Thank you for the assistance. |
You are right, sorry, I missed that |
I added more advanced support to const posts = await connection.getRepository(Post).find({ relations: ["photos", "user", "photos.user", "counters.author"] }); It was a hard feature to implement, because sub-relations can be joined via embeds, but I did it ;) |
* master: fixed tslint error removed only test fixes #1270 webpack: enable usage in node projects by not automatically selecting browser version updated changelog fixes #1241 driver(cordova): support extra options remove only added tests for #1261 fixes #1259 fixes #1259 fixes #1254; version bump fixes #1254 # Conflicts: # CHANGELOG.md # package.json # src/entity-manager/EntityManager.ts
OH WOW, excellent work @pleerock . Great to see that added! |
@pleerock, just to confirm, when using find options, is it limited to 2 levels? I don't see any examples anywhere of using more than 2 levels of relations and I am getting errors locally using {
"message": "Relation \"level1.level2.level3\" was not found, please check if it is correct and really exist in your entity."
} even though I can get to level 3 if I start at level 2 so I know the relationships are configured correctly. An example of what I am talking about would be: const posts = await connection.getRepository(Post).find({ relations: ["photos", "photos.user", "photos.user.profile"] }); where Thanks! |
And nevermind - I just came across #1504 and updated to |
@pleerock Just started using typeorm with graphql this weekend. Amazing library and Super Kudos 🚀 👍 🎉 on the nested If |
@jboothe thanks. If you want to use typeorm with graphql there is a framework Im currently working on - you can try it. |
Good to know. I'll check it out. |
This feature is super nice, but I can't find it in the documentation. Are the docs updated? Or am I missing something? |
@biels what docs are you talking about? I provided an example code above. |
Yes, thank you for that. But I shouldn't have to come to a github issue to learn about a feature, right? |
No, if you read all parts of the documentation. |
@pleerock Thanks for creating this library! We are starting to use it in a production environment.
It is very helpful to be able to fetch nth level relationships. However, it would be great if we could filter (maybe using .where) based on properties of the related objects. I have seen other issues where you mention that the queryBuilder is the tool for this case. But the problem with the query builder is that we wont have typed queries. Im assuming is a very hard feature to implement. Let me know if you have any plans on doing something like I mention. |
@pleerock |
I don't think this works anymore. Currently getting a typescript error when using sub-relations
|
It's very cool!) Yes, I know that this is my problem, but maybe can you put few examples in this section/topic |
@Evilart86 Contributions to make documentation better are always welcomed. |
I tried to load nested relations as the doc suggest but no matter what I do I always get an error. |
i try this code and got an error relation * was not found, so i change my code with this :
i remove the user entity and works fine |
I am not getting this feature. I have used it like this. Is there anything i am missing. I am only getting the first relation, that is user. The version of typeorm is 0.2.13
These are my entities
|
It's in the documentation but it's not working nor in the @next. I had a lot of interesting moments with it.
|
thanks @pleerock ! |
Does this works for next level too? like |
Hi, I wanna do something similar but then for mapping. I have a company entity, a reviews entity and an industry entity, I wanna get a company by Id with all the linked industries to it's linked reviews. I managed to get all the reviews with their industry using the following query:
Now what I actually want is to add a industries property to company that links all the industries linked to the review, any tips? |
FindRelationsNotFoundError: Relation "photos.url" was not found, please check if it is correct and really exist in your entity
Entitys
|
Excellent feature. using like this and worked well. |
you the best m8 |
Is there a way to use a relation to filter a query results?
|
I am getting:
When switchting from: return StrategyEntity.findOne({
relations: ['account'],
where: {
id: strategyId,
account: {
owner: {
userId: query.userId,
platform: query.platform,
},
},
},
}); to: return StrategyEntity.findOne({
relations: ['account', 'account.owner'],
where: {
id: strategyId,
account: {
owner: {
userId: query.userId,
platform: query.platform,
},
},
},
}); Tested with: typeorm v0.3.0-rc.19 |
still so inefficient... :( |
Hi, I'm trying to achieve the same result using query builder but couldn't quite get it. Can anybody help how to do the same using querybuilder? |
@pleerock FTW. |
On my side it didn't work if the relationship was already eagerly loaded. That is, if in Note: I am using typeorm version ^0.2.32 |
Just stumbled on this issue and I was able to implement 3 levels relations using: .find({
where: {
id: userId,
},
relations: [
'subscriptionPlan',
'subscriptionPlan.subscriptionType',
'subscriptionPlan.subscriptionType.features'
],
}) |
me too :) |
How about many to many relations? Does this work? Users have many groups, groups have many roles.
Does not work: the code below: Users:
Groups:
|
Is it possible to do the same with load loadRelationIds? Example loadRelationIds: { relations: ['photos.user'] } to just load the ids also in a nested way? |
Doesn't work for me in a situation with "entity.relation.relation" case:
|
If I have a Person entity with a one-to-many relationship with a Home entity, and then the Home entity has a many-to-one relationship with a HomeType entity, is there a way to get the Home entities to include their HomeType data when selecting Person's and including their Home relations?
let personsWithHomes = await Person.find({ where: { id: id }, relations: ["homes"] })
This would return my Person records with their associated Home records, but the Home records do not include their associated HomeType. I tried adding the type as an eager loaded relationship, but it still does not get included.
Is there a way to specify the sub relations to include?
let personsWithHomes = await Person.find({ where: { id: id }, relations: ["homes", "homes.homeType"] })
The text was updated successfully, but these errors were encountered: