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.

Lazy relations are not saved, DEFAULT added to insert queries

See original GitHub issue

Issue type:

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

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] 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:

Saving a lazy relation does not read the id of the associated entity.

I have checked the tests and I cannot figure out whats wrong on my side. If I remove lazy relations, then everything is fine.

The issue is, I cannot remove it everywhere. I know its an anti-pattern but some relations are resolved dynamically on specific events to avoid unnecessary load.

I have a Feed entity with Promise<User> ManyToOneOneToMany relations. Both side uses Promise.

@Entity()
class Feed {
    @ManyToOne(t => User, u => u.feeds)
    @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
    user: Promise<User>;

    @RelationId((f: Feed) => f.user)
    userId: string;
}
@Entity()
class User {
    @OneToMany(t => Feed, f => f.user)
    @JoinColumn({ name: 'id', referencedColumnName: 'user_id' })
    feeds: Promise<Feed[]>;
}

const user = await connection.getRepository(User).findOneOrFail(id);

const feed = new Feed();
feed.user = Promise.resolve(user);

await connection.getRepository(Feed).save(feed);

// query: INSERT INTO `feed`(`user_id`) VALUES (DEFAULT)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
totalardcommented, Feb 11, 2020

Is there any solution?

4reactions
sebapatr96commented, Oct 16, 2019

Is there any workaround for this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Eager and Lazy Relations - typeorm - GitBook
Eager relations are loaded automatically each time you load entities from the database. ... Entities in lazy relations are loaded once you access...
Read more >
entity framework - Lazy loading properties after an insert
You must set entity properties after calling Create (or call something like an Init method which you pass the parameters into).
Read more >
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 ...
Read more >
5 Common Hibernate Mistakes That Cause Dozens of ...
That's obviously not the most efficient approach and probably not what you expected. You can easily fix that by changing the data type...
Read more >
PostgreSQL and typeorm - Tips, tricks and common issues
Use uuid v4; Using defaults in typeorm; Beware of cascading actions with an ORM; Beware eager and lazy loading; Returning too much data...
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