help me please. I really don't understand how it's works I have an entity called Product, with a ManyToMany relation with another entity called Car with cascade true flag enabled.
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number
@ManyToMany(() => Car, { cascade: true })
@JoinTable()
cars: Car[]
}
The Car entity doesn't contain a reference to the Product entity. That means like i think that the relation is unidirectional. So, when i save a product, i set the cars for this product:
const cars = await this.carService.getByIds(createProductDto.cars)
const newProduct = this.productsRepository.create({
...createProductDto,
cars
})
await this.productsRepository.save(newProduct)
I get the cars entities by ids and save them to the product. This works well. I'm not sure that it is the best way, but i don't know another.
The problems comes when i try to update some existing product in the db. I get the fallowing errors:
ERROR [ExceptionsHandler] duplicate key value violates unique constraint "PK_f7b49d8fd8d09ea2fc2e7542f3f"
or
Error: Cannot query across many-to-many for property cars
or other... Help me please. I want to understand what is the best way to work with such relations. What is the best way to create, update and remove it. Thanks