question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading relationships from existing entity instance inside the same instance

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem: It is possible to load relationships using an existing entity instance?

For example, I have an User entity with OneToMany relationship with the Reservation entity. Then I have a user (user = User.find(1) ) and I want the reservations property inside the user instance object like so(with no eager loading, lazy loading or query builder):

let user = User.find(1);
// do some processing
return user; 
/* 
expect: {
id : 1,
email : 'some@email'
}
*/
// load reservations
user.load('reservations')

return user;
/* 
expect: {
id : 1,
email : 'some@email',
reservations: [{etc},{etc},{etc}]
}
*/

PS. I cannot use eager loading because I will get circular error. The only option I see is by using lazy loading and adding the reservations array in my user instance like so:

let user = User.find(1);
let reservations = await user.reservations // after setting lazy loading in my entity
user.reservations = reservations;
return user;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

29reactions
IonelLupucommented, Oct 6, 2020

This is funny. A few moments ago I found myself needing this feature and after a quick google I found out that I was the person who posted this issue on Github. lol

Maybe it needs to be added on a priority list or something? @pleerock

23reactions
Matgsancommented, May 21, 2020

Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading Related Entities - EF6 - Microsoft Learn
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic ......
Read more >
EF: Attaching entity to context and assigning to new instance ...
So in regards to my question, doing what I mentioned would changed the entry state to Detached which will not update on a...
Read more >
Working with entity instances — Pony ORM documentation
The relationships between two objects are defined by two relationship attributes. If another side of the relationship is declared as a Set ,...
Read more >
Define relationships between objects - Android Developers
Finally, add a method to the DAO class that returns all instances of the data class that pairs the parent entity and the...
Read more >
Object Relational Tutorial (1.x API)
For example, below we create a new Query object which loads instances of User . ... already-persisted object with the same primary key...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found