admin管理员组

文章数量:1386925

Let's suppose I have an entity:

@OneToMany(
    (): typeof UserCarData => UserCarData,
    (userCar: UserCarData): User => userCar.user,
    {
      cascade: [Cascade.ALL],
    },
  )
  public userCars = new Collection<UserCarData>(this);

  @OneToMany((): typeof Auth => Auth, (auth: Auth): User => auth.user, {
    cascade: [Cascade.ALL],
  })
  public auth = new Collection<Auth>(this);

  @Unique()
  @Property({ columnType: 'text' })
  public email: string;

When I try to create a new record like this:

const entityToCreate = this.em.create(User, {
      ...plainToInstance(User, dto),
      password: hashedPassword,
      accessTokenVersion: 0,
    });

I get such object:

{
  userCars: Collection<UserCarData> { initialized: true, dirty: false },
  auth: Collection<Auth> { initialized: true, dirty: false },
  email: '[email protected]',
  role: 'car_owner',
  password: '$argon2id$v=19$m=65536,t=3,p=4$Voe9E9r7vWQzAM/qY0NUIw$uhiYSCO/V2j3gX3VjA7WXEQ+L3Z7PMfX6eh6AtA9VCY',
  accessTokenVersion: 0
}

The problem is that during creational transaction MicroORM makes two additional queries:

update "auth" set "user_id" = '27' where "id" is null
update "user_car_data" set "user_id" = '27' where "id" is null

The question is how to get rid of those queries? Manually set to null collections before creation?

Let's suppose I have an entity:

@OneToMany(
    (): typeof UserCarData => UserCarData,
    (userCar: UserCarData): User => userCar.user,
    {
      cascade: [Cascade.ALL],
    },
  )
  public userCars = new Collection<UserCarData>(this);

  @OneToMany((): typeof Auth => Auth, (auth: Auth): User => auth.user, {
    cascade: [Cascade.ALL],
  })
  public auth = new Collection<Auth>(this);

  @Unique()
  @Property({ columnType: 'text' })
  public email: string;

When I try to create a new record like this:

const entityToCreate = this.em.create(User, {
      ...plainToInstance(User, dto),
      password: hashedPassword,
      accessTokenVersion: 0,
    });

I get such object:

{
  userCars: Collection<UserCarData> { initialized: true, dirty: false },
  auth: Collection<Auth> { initialized: true, dirty: false },
  email: '[email protected]',
  role: 'car_owner',
  password: '$argon2id$v=19$m=65536,t=3,p=4$Voe9E9r7vWQzAM/qY0NUIw$uhiYSCO/V2j3gX3VjA7WXEQ+L3Z7PMfX6eh6AtA9VCY',
  accessTokenVersion: 0
}

The problem is that during creational transaction MicroORM makes two additional queries:

update "auth" set "user_id" = '27' where "id" is null
update "user_car_data" set "user_id" = '27' where "id" is null

The question is how to get rid of those queries? Manually set to null collections before creation?

Share Improve this question edited Mar 18 at 21:26 xDD asked Mar 18 at 20:45 xDDxDD 275 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Using plainToInstance(User, dto) causes the problem

本文标签: nestjsMikroORM behaviour of Collection during creatingStack Overflow