Reputation: 25
I am using TypeORM for the first time and I got stuck trying to get an entry from another table using a FK. But I get this Error.
Error: Relation "ingredientCategoryId" was not found; please check if it is correct and really exists in your entity.
ingredient.entity
@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;
}
ingredientsCategory.entity
@Entity('ingredientsCategory')
export class IngredientsCategoryEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToMany(
() => IngredientEntity,
(ingredient) => ingredient.ingredientsCategory,
)
ingredient: IngredientEntity[];
}
ingredient.service
async getIngredientCategory(id: number): Promise<any> {
const ingredient: IngredientEntity = await this.repo.findOne({
where: { id: id },
relations: ['ingredientCategoryId'],
});
return ingredient.ingredientsCategory;
}
ingredientsCategory.controller
@Get('/:id/ingredientsCategoryList')
async getCategory(@Param('id', ParseIntPipe) id: number) {
return this.ingredientService.getIngredientCategory(id);
}
Upvotes: 1
Views: 11295
Reputation: 6665
Try this:
relations: ['ingredientsCategory'],
instead of:
relations: ['ingredientCategoryId'],
Upvotes: 2