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

668
Vistas
How can I cascade delete with one-to-many relation in typeORM?

I have group and contact entities in OneToMany relationship. I add contacts within the group. When I want to delete, I want both sides of the database to be deleted. but only the group table is deleted.

here's contact.entity contact.entity

Here's one to many relationship:

here's group.entity group.entity

The function I wrote in the service is as follows: function in the service

I tried many solutions by searching. Adding cascades on both sides, I know this isn't right, but I wanted to try, but I couldn't get it to work.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you are using it wrong way. If you hover to softDelete function, you will see it need props like: (method) Repository<Group>.softDelete(criteria: string | number | Date | ObjectID | string[] | FindConditions<Group> | number[] | Date[] | ObjectID[] hovered

which mean you have to use it like :

getRepository(Group).softDelete({ id: groupId})

same as update function: update

getRepository(Group).update({id: groupId}, {name: "new name"})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't hard code something it can be done by the database itself. If you want all the contacts of the group to be deleted while the group is deleted then use foreign key with

on delete cascade

eg:

create table group1(
id serial primary key,
name varchar
);

create table contact(
id serial primary key,
name varchar,
group_id integer references group1 (id) on delete cascade
);

Result here

about 4 years ago · Juan Pablo Isaza Denunciar
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