Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
TypeORM: How to do Many to Many relationshop trows error when object already exists

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;
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!