TypeORM does not have the capability to effectively remove a row when there is a ManyToOne or

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

Answer №1

The issue stemmed from using conn.getRepository(Customer) when it should have been conn.getRepository(Visit).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize an Angular HttpInterceptor to invoke a Promise

I have an angular HttpInterceptor and I am in need of invoking an encryption method that is defined as follows: private async encrypt(obj: any): Promise<string> { However, I am unsure of how to handle this within the HttpInterceptor: intercept(req ...

Establishing the Time-to-Live (TTL) with Redis-OM and Node Object Mapping

Recently, I stumbled upon the exciting new Redis-OM Node Object Mapping feature. Although I have limited experience with Redis, I am eager to dive into it now. Currently, I have a basic function in place for creating rooms but I want these rooms to automa ...

Transforming a large JSON dataset into a CSV format

Seeking help with converting a massive JSON file containing tweet data (48MB) to CSV format for importing into a SQL database and cleansing purposes. Despite attempting multiple JSON to CSV converters, they all error due to the file size. Is there an effi ...

The Extended Date type is indicating that the function cannot be located

I came across this helpful gist with date extensions: https://gist.github.com/weslleih/b6e7628416a052963349494747aed659 However, when attempting to use the functions, I encountered a runtime error stating: TypeError: x.isToday is not a function I foun ...

NPM: There are no valid TypeScript file rules specified

Currently working on a small project using React.JS. Whenever I execute : npm run start, the following message gets logged: Starting type checking and linting service... Using 1 worker with 2048MB memory limit Watching: /Users/John/Projects/myProject/src ...

SQL querying with multiple conditions and selected fields

I have carefully organized data in 2 tables with the following structure: Meetings meet_id meet_category Orders order_id meet_id order_date I am seeking to construct a single query that can provide the total number of meetings, along with the ...

Inject components in Angular using dependency injection

Can components in Angular be dependency injected? I am interested in a solution similar to injecting services, like the example below: my.module.ts: providers: [ { provide: MyService, useClass: CustomService } ] I attempted using *ngIf= ...

Having trouble passing a React Router Link component into the MuiLink within the theme

The MUI documentation explains that in order to utilize MuiLink as a component while also utilizing the routing capabilities of React Router, you need to include it as a Global theme link within your theme. An example is provided: import * as React from & ...

Error encountered in Snap SVG combined with Typescript and Webpack: "Encountered the error 'Cannot read property 'on' of undefined'"

I am currently working on an Angular 4 app that utilizes Snap SVG, but I keep encountering the frustrating webpack issue "Cannot read property 'on' of undefined". One solution I found is to use snapsvg-cjs, however, this means losing out on the ...

Declaration files for Typescript ESLint configurations

I've been researching this issue online, but I haven't been able to find any solutions. It could be because I'm not entirely sure what's causing the problem. What I'm trying to do is set a global value on the Node.js global object ...

Tips for executing a type-secure object mapping in Typescript

I am currently working on creating a function in Typescript that will map an object while ensuring that it retains the same keys. I have attempted different methods, but none seem to work as intended: function mapObject1<K extends PropertyKey, A, B>( ...

Arranging Angular Cards alphabetically by First Name and Last Name

I am working with a set of 6 cards that contain basic user information such as first name, last name, and email. On the Users Details Page, I need to implement a dropdown menu with two sorting options: one for sorting by first name and another for sorting ...

Can you explain the purpose of the curly braces found in a function's parameter definition?

I am currently working on an Angular 5 project and came across this intriguing Typescript code snippet. (method) CreateFlightComponent.handleSave({ currentValue, stepIndex }: { currentValue: Partial<Flight>; stepIndex: number; }): void Can ...

Consecutive HTTP requests in Angular using rxjs

Currently working on a function that utilizes concatMap to perform sequential HTTP calls, such as adding a person, using the returned information to add a contact, and then adding some accounts. This function takes in a list (in my case, portfolios) and f ...

Issue with displaying Typescript sourcemaps on Android device in Ionic 2

Encountering a strange behavior with Ionic2. After deploying my app to a simulator, I am able to view the .ts file sourceMap in the Chrome inspect debugger. In both scenarios, I use: ionic run android https://i.sstatic.net/JarmI.png However, when depl ...

Moment-Timezone defaults to the locale settings after the global Moment locale has been established

I am currently developing an application using Typescript that requires features from both Moment.js and moment-timezone. To localize the date and timestamps within the application, I have set moment's locale in the main app.ts file to match the langu ...

Angular: The Ultimate Guide to Reloading a Specific Section of HTML (Form/Div/Table)

On my create operation page, I have a form with two fields. When I reload the page using window.reload in code, I can see updates in the form. However, I want to trigger a refresh on the form by clicking a button. I need help writing a function that can r ...

Developing a custom camera system for a top-down RPG game using Javascript Canvas

What specific question do I have to ask now? My goal is to implement a "viewport" camera effect that will track the player without moving the background I am integrating websocket support and planning to render additional characters on the map - movement ...

You need to provide 1 type argument(s) for the Generic type ModuleWithProviders<T> in Angular 10 Swagger Codegen

Currently, I am generating Codegen proxies using . Upon implementing this in Angular 10, I encountered the following error. How can this issue be resolved? The error message reads: 'Generic type 'ModuleWithProviders' requires 1 type argume ...

Converting Typescript fat arrow syntax to regular Javascript syntax

I recently started learning typescript and I'm having trouble understanding the => arrow function. Could someone clarify the meaning of this JavaScript code snippet for me: this.dropDownFilter = values => values.filter(option => option.value ...