我是第一次使用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);
}
1条答案
按热度按时间yhxst69z1#
试试这个:
而不是: