I'm currently grappling with a problem that has me stumped. I've spent countless hours trying to find a solution, but to no avail. I'm using MS-SQL on Azure.
The structure of my entities is as follows:
- Customer and Visits: OneToMany (Primary)
- Visits and Customers: ManyToOne (Inverse)
I've implemented soft-deletes for my customers, allowing me to retrieve visit information even when customer data is no longer available. I'm hesitant to use "Cascade DELETE" for this reason.
However, I'm encountering issues when trying to completely delete visits (as opposed to soft-deleting customers). I suspect foreign key constraints might be causing this, although I'm not seeing any error messages from TypeORM. The DeleteResult.affected property and my DataGrip queries both show 0 when attempting to delete visits.
Interestingly, I am able to manually delete a row using a simple SQL statement like the following:
DELETE FROM visits
WHERE uuid = 'f0ea300d-...-656a'
My entity setup is as follows (irrelevant information omitted):
@Entity({ name: 'customers' })
export class Customer {
@PrimaryColumn()
uuid: string
@OneToMany(() => Visit, (visit) => visit.customer)
visits?: Visit[]
}
@Entity({ name: 'visits' })
export class Visit {
@PrimaryColumn()
uuid: string
@ManyToOne(() => Customer, (customer) => customer.visits)
customer: Customer
}
My GraphQL resolver:
@Mutation(() => Boolean)
async deleteVisitsByUuid(
@Arg('uuid') uuid: string,
@Ctx() { conn }: AppContext,
): Promise<boolean> {
const repo = conn.getRepository(Customer)
const result = await repo.delete(uuid)
const affected = result.affected
if (affected === undefined || affected == null) {
return false
} else {
return affected > 0
}
}