RobinCK/typeorm-fixtures

[Bug] TypeOrm Lazy relations are not properly handled

Closed this issue · 5 comments

If you have relations defined as lazy, i.e. like:

@Entity()
class User {
  @OneToOne('Profile', 'user')
  profile: Promise<Profile>
}

Then when you run fixtures - all relation fields are nulls.
No exceptions thrown during fixtures loading.

TypeOrm Lazy Relations doc: https://typeorm.io/#/eager-and-lazy-relations/lazy-relations

Your Environment

Software Version(s)
typeorm 0.2.25
typeorm-fixtures 1.6.0
Node v12.16.3
npm/Yarn 6.14.5
Operating System Linux

Hey @RobinCK ,
I tried to find a workaround by using processors, but it's not working, too.

account.yaml:

entity: Account
processor: processors/account.processor.ts
items:
  account1:
    app: test_app
    roles:
      - user
    user: '@user1'

processors/account.processor.ts:

import { IProcessor } from 'typeorm-fixtures-cli/dist';
import { Account } from '../../src/account';

export default class AccountProcessor implements IProcessor<Account>{
  preProcess(name: string, object: Account): any {
    object.user = Promise.resolve(object.user);
    return object;
  }
}

Account table still has userId set to null.

Hey @RobinCK
did you had a chance to take a look at this issue?
probably there is no good fix, but rather some workaround you can imagine?

This is related to typeORM itself: typeorm/typeorm#2276 and will probably be resolved in due time.
The thing about Promise.resolve, is that it also returns a promise so essentially you're assigning a promise object, rather than its value.

In theory, you could have assigned the resolved value, but then typescript will fail as it expects a promise, so it's sort of a double bind where declaring a promise expects lazy evaluation, but the entities themselves don't handle it and still expect the actual value when assigning their relational entities.

For now, you can define your entities like this:

@Entity()
class User {
  @OneToOne('Profile', 'user', { lazy: true })
  profile: Profile
}

and await for the relation like this: object.user = await object.user;
or this:

const user = await object.user;
object.user = user;

The bad part here is that the types don't really represent the proper values, i.e Profile is actually Promise<Profile> with the lazy flag on.
However, at least for now, this will enable you to pass values to your entities while still loading it lazily.

Dear all its a limitation of typeorm but it has to be solved also by the promise loader.

The main limitation is due to the class-transformer package which transforms promises in property names like __promise_<property>__ and results in a bad typeorm interpretation. It is used is in the Builder.js package.

I proposed a solution there:

#159

Regards

Hello
I am still facing this issue, any solution?