question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cascade deleting works incorrect

See original GitHub issue

There is a small problem with cascade deleting.

I have an entity Chart with one to many relation:

@OneToMany(_type => ChartRow, row => row.chart, { cascadeAll: true })
public rows: ChartRow[]

The problem is when I try to remove this entity, I got constraint fails because the related entity are deleted after the main entity:

executing query: START TRANSACTION                                                                                                                                                         
executing query: DELETE FROM chart WHERE id=? -- PARAMETERS: [6]                                                                                                                           
executing query: DELETE FROM chart_row WHERE id=? -- PARAMETERS: [8]                                                                                                                       
executing query: DELETE FROM chart_row WHERE id=? -- PARAMETERS: [9]                                                                                                                       
executing query: DELETE FROM chart_row WHERE id=? -- PARAMETERS: [10]                                                                                                                      
query failed: DELETE FROM chart WHERE id=? -- PARAMETERS: [6]                                                                                                                              
error during executing query:Error: ER_ROW_IS_REFERENCED_2: Cannot delete or update a parent row: a foreign key constraint fails (`typeorm`.`chart_row`, CONSTRAINT `fk_79c9460c3e56c4b2119
675883d8` FOREIGN KEY (`chart`) REFERENCES `chart` (`id`))                                                                                                                                 
executing query: ROLLBACK                                        

So I tried to do cascade delete by database and added onDelete: "CASCADE":

@OneToMany(_type => ChartRow, row => row.chart, { cascadeAll: true, onDelete: "CASCADE" })

But the schema sync didn’t create the DDL alter table - it still have ON DELETE RESTRICT which don’t let me to delete this entity with the same error.

I can do the workaround and delete it manually:

await Promise.all(chart.rows.map(row => this.chartRowRepository.remove(row)));

But it’s not a point of using a relation db and ORM 😉

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

35reactions
pleerockcommented, Nov 26, 2016

I noticed one thing:

@OneToMany(_type => ChartRow, row => row.chart, { cascadeAll: true, onDelete: "CASCADE" })

onDelete: "CASCADE" should be set on inverse side, .e.g. on @ManyToOne side.

I havent tested this on master brach, but I did it my own branch and it works. My own branch contains changes for the next version of typeorm.

1reaction
MichalLytekcommented, Nov 26, 2016

I changed it to:

class ChartRow {
    @ManyToOne(_type => Chart, chart => chart.rows, { nullable: false , onDelete: "CASCADE"})
    public chart: Chart;
}

But the migration didn’t altered the fk and it’s still ONDELETE: Restrict. I’m using the 0.0.2-alpha.70 version so maybe your new persistence mechanism works better 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cascading delete problem - Power Platform Community
Hi,. I have a table (Project) that is parent to a bunch of other tables. I have configured one-to-many relationships with them all...
Read more >
Good explanation of cascade (ON DELETE/UPDATE) behavior
CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well. There are two other options:...
Read more >
Why ON CASCADE DELETE is not working? - Stack Overflow
This is removing all rows from form_questionnaire but leaves rows in questionnaire . Why rows in questionnaire are not removed if there is...
Read more >
Cascade Delete in Entity Framework 6
Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database.
Read more >
Why you should avoid CascadeType.REMOVE for to-many ...
Most developers worry about deleting too many database records when they use CascadeType.REMOVE. And that's definitely an issue. But it's not the only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found