Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda