RobinCK / typeorm-fixtures

:pill: Fixtures loader for typeorm 🇺🇦
https://robinck.github.io/typeorm-fixtures/
MIT License
566 stars 45 forks source link

[Bug] TypeOrm Lazy relations are not properly handled #143

Closed ygarris closed 4 years ago

ygarris commented 4 years ago

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
ygarris commented 4 years ago

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.

ygarris commented 4 years ago

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?

silicakes commented 4 years ago

This is related to typeORM itself: https://github.com/typeorm/typeorm/issues/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.

nmacherey commented 3 years ago

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:

https://github.com/RobinCK/typeorm-fixtures/pull/159

Regards

mehdibo commented 1 year ago

Hello I am still facing this issue, any solution?