Cannot assign object to lazy relation (expects promise)
See original GitHub issueIssue type:
(I’m really not sure if this is an issue with the library, documentation, or just my understanding)
[ x ] question [ x ] bug report [ ] feature request [ x ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[ x ] 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:
Entities
@Entity()
export class Role {
@ManyToOne(type => Store, store => store.roles)
store: Promise<Store>;
}
@Entity()
export class Store {
@OneToMany(type => Role, role => role.store)
roles: Promise<Role[]>;
}
This code fails because the types expect store to be a promise
const r = roleRepository.create();
r.store = await storeRepository.findOne();
await roleRepository.save(r);
This code fails because the implementation expects an object (not a promise)
const r = roleRepository.create();
r.store = storeRepository.findOne();
await roleRepository.save(r);
This code works, but is clearly ugly / hacky
const r = roleRepository.create();
r.store = <any>(await storeRepository.findOne());
await roleRepository.save(r);
What is the correct way to achieve this?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Why i cannot assign new key value in nested array object that ...
Strange how you mutate the state, maybe try the following: addBase64Function(values = []) { //always return a promise, even if values is ...
Read more >Promise - JavaScript - MDN Web Docs
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Read more >R Language Definition
Promise objects are part of R's lazy evaluation mechanism. They contain three slots: a value, an expression, and an environment. When a function...
Read more >Router tutorial: tour of heroes - Angular
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >Don't let Entity Framework call the shots - Fear of Oblivion
If there is one thing you take away from this post, it is that EF is there to map data from the database...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Another way to achieve this is to change the entity to…
However, this isn’t explained in the documentation anywhere. The section about this says to just use
store: Promise<Store>
which would never work for assigning values as far as I can tell.This solution seems dirty too. It allows me to bypass the compile time type check and do things that will break at run time due to invalid type (i.e. I could still pass a promise to
role.store
or try to read it as an object when querying).Update:
After trying this I realized that with the entity typed like this If I do
role.store
it is undefined.Unless I’m missing something this is a fatal flaw with the design of lazy relations which makes it entirely unusable.
Hello I’m experiencing this issue. My code
If I print an
entity
I get this, which is wrong and fails upon saveOnly workaround is manualy set property, like this
entity
printedIt’s working, but it’s it’s too ugly…