Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda