Skip to content
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

Entity save fails after lazy loading relations #3689

Open
AnderoKoplus opened this issue Feb 22, 2019 · 4 comments
Open

Entity save fails after lazy loading relations #3689

AnderoKoplus opened this issue Feb 22, 2019 · 4 comments

Comments

@AnderoKoplus
Copy link

Issue type:

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

Database system/driver:

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

TypeORM version:

[x] latest
[ ] @next
[x] 0.2.12 and 0.2.13 (or put your version here)

Steps to reproduce or a small repository showing the problem:

import { Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class FooBar {
    @PrimaryGeneratedColumn()
    public id!: number;

    @ManyToMany((type) => FooBar, (fooBar) => fooBar.competitors)
    @JoinTable()
    public competitors!: Promise<FooBar[]>;
}

    const fooBar = await getManager().getRepository(FooBar).findOne(1);
    const c = await fooBar.competitors; // lazy loading causes ORM to fail
    const a = await getManager().getRepository(FooBar).save(fooBar);

TypeError: Cannot read property 'find' of undefined
    at persistence\subject-builder\ManyToManySubjectBuilder.ts:152:75
    at Array.forEach (<anonymous>)
    at ManyToManySubjectBuilder.buildForSubjectRelation (persistence\subject-builder\ManyToManySubjectBuilder.ts:117:25)
    at persistence\subject-builder\ManyToManySubjectBuilder.ts:45:22
    at Array.forEach (<anonymous>)
    at persistence\subject-builder\ManyToManySubjectBuilder.ts:39:50
    at Array.forEach (<anonymous>)
    at ManyToManySubjectBuilder.build (persistence\subject-builder\ManyToManySubjectBuilder.ts:32:23)
    at EntityPersistExecutor.<anonymous> (persistence\EntityPersistExecutor.ts:106:64)
    at step (node_modules\tslib\tslib.js:133:27)

it works with 0.2.11

@ghost
Copy link

ghost commented Feb 24, 2019

Lazy-Loading with Promises was abandonded in the end of 2018. There sadly will be no further support and i also do have mny problems with Lazy-Loading, which seem to be never solved.
See this comment as ref:
#2902 (comment)

@johannesschobel
Copy link
Contributor

Wait.. do you say, that lazy-loading of relationships is deprecated? 😱

@ghost
Copy link

ghost commented Mar 3, 2019

It's not that simple, i now know...
See this: #3703
I personally will abandon lazyloading and just switch to eager loading. You can still control if the relations are loaded or not by putting "relations: ['myRelation']" into the query parameters...

@johannesschobel
Copy link
Contributor

hm, good to know.. i come from the "Laravel universe" and relationships are 99% lazy-loaded - except if you explicitly load them eager.. So this was the "go to strategy" in Laravel - and therefore i am quite familiar with it.. I know, lazy loading has several downsides, for example, it may result in a lot of extra queries (e.g., for complex, deeply nested structures) which may be easier to get / load if you query it eagerly (e.g., everything is get within one single but more complex query)

Thanks for linking to the other issue!

SonNg2k added a commit to 2359vn-internships/mstory-server that referenced this issue Nov 25, 2020
This is an unstable and error-prone way to achive lazy-loading. Source: typeorm/typeorm#3689
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants