The problem I want to solve is that when creating a product I want to associate a List of categories and When creating those products if the category doesn't exist then create it. The problem is when I want to create another product and associate a Category it throws an error that there is already a cateogory with that name as expected.
This is my Category entity
import { Category } from 'src/categories/entities/category.entity';
import {
Column,
Entity, ManyToMany, PrimaryGeneratedColumn
} from 'typeorm';
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(()=>Category,category=>category.products,{cascade:true})
categories: Category[];
@Column()
barCode: string;
}
and this is my product entity
import { Category } from 'src/categories/entities/category.entity';
import {
Column,
Entity, ManyToMany, PrimaryGeneratedColumn
} from 'typeorm';
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(()=>Category,category=>category.products,{cascade:true})
categories: Category[];
@Column()
barCode: string;
}