postgresql 类型表单如何在findOne()中使用关系

qyyhg6bp  于 2022-11-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(149)

我是第一次使用TypeORM,我在尝试使用FK从另一个表中获取条目时遇到了麻烦。但是我收到了这个错误。
错误:找不到关系“”;请检查它是否正确以及是否确实存在于实体中。

成分.实体

@Entity('ingredient')
export class IngredientEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false })
  ingredientCategoryId: number;

  @ManyToOne(
    () => IngredientsCategoryEntity,
    (ingredientsCategory) => ingredientsCategory.ingredient,
    { eager: true },
  )
  @JoinColumn({ name: 'ingredientCategoryId', referencedColumnName: 'id' })
  ingredientsCategory: IngredientsCategoryEntity;
}

成分类别实体

@Entity('ingredientsCategory')
export class IngredientsCategoryEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToMany(
    () => IngredientEntity,
    (ingredient) => ingredient.ingredientsCategory,
  )
  ingredient: IngredientEntity[];
}

配料.服务

async getIngredientCategory(id: number): Promise<any> {
    const ingredient: IngredientEntity = await this.repo.findOne({
      where: { id: id },
      relations: ['ingredientCategoryId'],
    });
    return ingredient.ingredientsCategory;
  }

配料类别控制器

@Get('/:id/ingredientsCategoryList')
  async getCategory(@Param('id', ParseIntPipe) id: number) {
    return this.ingredientService.getIngredientCategory(id);
  }
yhxst69z

yhxst69z1#

试试这个:

relations: ['ingredientsCategory'],

而不是:

relations: ['ingredientCategoryId'],

相关问题