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

321
Vistas
TypeORM Cannot delete data with @OneToMany / @ManyToOne relation using QueryBuilder

I am currently using TypeORM and TypeScript and try to delete data. Since I want to delete based on the owner of 'Customer' I thought I'd had to use JOINS. Since I am not very fluent with more advanced concepts like JOINS in SQL, I would appreciate any help with a small explanation. Thank you.

The following code throws this exception:

"message": "\"customer\" alias was not found. Maybe you forgot to join it?",
@Resolver()
export class RootResolver {

  @Mutation(() => Boolean)
  async deleteAllDataByOwner(
    @Arg('ownerEmail') ownerEmail: string,
    @Ctx() { conn }: AppContext,
  ): Promise<boolean | undefined> {

    const deleteVisitsPromise = conn.createQueryBuilder()
      .leftJoinAndSelect("customer.visits", "visit")
      .delete()
      .from(Customer)
      .where("customer.owner = :owner", { owner: ownerEmail })
      .execute()

    const deleteCustomersPromise = conn.createQueryBuilder()
      .delete()
      .from(Customer)
      .where("customer.owner = :owner", { owner: ownerEmail })
      .execute()

    return await Promise.all(
      [deleteCustomersPromise, deleteVisitsPromise]
    ).then(_ => {
      return true
    })
  }
}
@ObjectType()
@Entity({ name: 'customers' })
export class Customer {
  @Field()
  @PrimaryColumn()
  uuid: string

  @Field(() => [Visit], { nullable: true })
  @OneToMany(() => Visit, (visit) => visit.customer)
  visits?: (Visit | null)[]
  
  // ...
}
@ObjectType()
@Entity({ name: 'visits' })
export class Visit {
  @Field()
  @PrimaryColumn()
  uuid: string

  @Field(() => Customer)
  @ManyToOne(() => Customer, (customer) => customer.visits, { 
    onDelete: 'CASCADE'
  })
  customer: Customer

  // ...
}
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