当我尝试在 TypeORM 中创建多对多关系时,我遇到了一些麻烦。我正在尝试在食谱中添加一种成分。我的数据库(Postgres)中已经有一个食谱和两种成分。当我尝试在配方和成分1 之间建立关系时,它工作得很好。但是,当我尝试在配方和成分2 之间添加新关系时,它实际上更新了第一个关系,而不是在我的联接表中创建新关系。
我正在尝试执行此请求:PUT /recipes/:recipeId/ingredients/:ingredientId。(它也不适用于 POST 方法)
这是我的代码:
成分.entity.ts
@Entity('ingredients')
class Ingredient {
@PrimaryGeneratedColumn('uuid', { name: 'ingredient_id' })
ingredientId: string;
@Column({ name: 'ingredient_name' })
ingredientName: string;
@Column({
name: 'ingredient_amount',
type: 'decimal',
precision: 12,
scale: 2,
})
ingredientAmount: number;
@Column({
name: 'ingredient_unit',
type: 'enum',
enum: ['un', 'g', 'ml'],
})
ingredientUnit: IngredientUnitType;
@Column({
name: 'ingredient_price',
type: 'decimal',
precision: 12,
scale: 2,
})
ingredientPrice: number;
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
食谱实体.ts
@Entity('recipes')
class Recipe {
@PrimaryGeneratedColumn('uuid', { name: 'recipe_id' })
recipeId: string;
@Column({ name: 'recipe_name' })
recipeName: string;
@Column({
name: 'recipe_cost',
type: 'decimal',
precision: 12,
scale: 2,
})
recipeCost: number;
@ManyToMany(() => Ingredient)
@JoinTable()
ingredients: Ingredient[];
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
}
食谱配料.service.ts
@injectable()
class RecipesIngredientsService {
constructor(
@inject('RecipesRepository')
private recipesRepository: RecipesRepository,
@inject('IngredientsRepository')
private ingredientsRepository: IngredientsRepository,
) {}
public async execute({ recipeId, ingredientId }: IRequest): Promise<Recipe> {
const recipe = await this.recipesRepository.findById(recipeId);
const ingredient = await this.ingredientsRepository.findById(ingredientId);
if (!recipe || !ingredient) {
throw new Error('Something bad happened');
}
recipe.ingredients = [ingredient];
await this.recipesRepository.save(recipe);
return recipe;
}
}
食谱.repository.ts
class RecipesRepository {
private ormRepository: Repository<Recipe>;
constructor() {
this.ormRepository = getRepository(Recipe);
}
public async create({ recipeName }: CreateRecipeDTO): Promise<Recipe> {
const recipe = this.ormRepository.create({
recipeName,
});
await this.ormRepository.save(recipe);
return recipe;
}
public async findById(recipeId: string): Promise<Recipe | undefined> {
const findById = await this.ormRepository.findOne({
where: {
recipeId,
},
});
return findById || undefined;
}
public async findAll(): Promise<Recipe[]> {
const recipes = await this.ormRepository.find();
return recipes;
}
public async save(recipe: Recipe): Promise<Recipe> {
return this.ormRepository.save(recipe);
}
}
成分.repository.ts
class IngredientsRepository {
private ormRepository: Repository<Ingredient>;
constructor() {
this.ormRepository = getRepository(Ingredient);
}
public async create({
ingredientName,
ingredientAmount,
ingredientUnit,
ingredientPrice,
}: CreateIngredientDTO): Promise<Ingredient> {
const ingredient = this.ormRepository.create({
ingredientName,
ingredientAmount,
ingredientUnit,
ingredientPrice,
});
await this.ormRepository.save(ingredient);
return ingredient;
}
public async findById(ingredientId: string): Promise<Ingredient | undefined> {
const findById = await this.ormRepository.findOne({
where: {
ingredientId,
},
});
return findById || undefined;
}
public async findAll(): Promise<Ingredient[]> {
const ingredients = await this.ormRepository.find();
return ingredients;
}
}
我错过了什么?
谢谢你们!