Cascade deleting works incorrect
See original GitHub issueThere 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:
- Created 7 years ago
- Comments:7 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
I noticed one thing:
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.
I changed it to:
But the migration didn’t altered the fk and it’s still
ONDELETE: Restrict
. I’m using the0.0.2-alpha.70
version so maybe your new persistence mechanism works better 😉