Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM - One to Many: EntityColumnNotFound: No entity column "context.physicalPresets" was found

Tags:

typeorm

I get the following error when updating Project entity: EntityColumnNotFound: No entity column "context.physicalPresets" was found.

ProjectContext is embeded in Project as described here https://typeorm.io/#/embedded-entities. ProjectContext has a OneToMany relation to Physical entity.

@Entity()
export class Project extends BaseEntityModel {
  // embedded
  @Column(type => ProjectContext)
  context: ProjectContext;
}
// embedded entity
@Entity()
export class ProjectContext {
  @OneToMany(type => Physical, physical => physical.project, {
    eager: true,
  })
  @JoinColumn()
  physicalPresets: Physical[];
}
@Entity()
export class Physical extends BaseEntityModel {
  @ManyToOne(
    type => Project, project => project.context.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  project: Project;
}

Not more success with:

@Entity()
export class Physical extends BaseEntityModel {
 @ManyToOne(
    type => ProjectContext, projectContext => projectContext.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  projectContext: ProjectContext;
}

I get Entity metadata for Physical#projectContext was not found. Check if you specified a correct entity object and if it's connected in the connection options.

  async update(entity: Project): Promise<UpdateResult> {
    return await this.repository.update(entity.id, entity);
  }

Is it possible to have OneToMany relationship in an embed entity, if yes how?

like image 735
Ambroise Rabier Avatar asked Feb 27 '19 11:02

Ambroise Rabier


1 Answers

As the documentation explain, the update method of Repository:

Updates entity partially. Entity can be found by a given conditions. Unlike save method executes a primitive operation without cascades, relations and other operations included.

Using save method solve No entity column [...] was found.

  async update(entity: Project): Promise<Project> {
    return await this.repository.save(entity);
  }

This is the correct way of doing OneToMany with embedded entity.

@Entity()
export class Physical extends BaseEntityModel {
  @ManyToOne(
    type => Project, project => project.context.physicalPresets,
    {onDelete: 'CASCADE'},
  )
  project: Project;
}

See also: https://github.com/typeorm/typeorm/blob/master/test/functional/cascades/cascade-insert-from-both-sides/cascade-insert-from-both-sides.ts

like image 148
Ambroise Rabier Avatar answered Sep 28 '22 07:09

Ambroise Rabier