sequelize: cascade delete does not work
i associated lines with orders like following but I want to delete them as relational but it didn’t work, what could be reason for this ?
associate: function(models) {
Orders.hasMany(models.lines, { onDelete: 'cascade' });
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 23 (2 by maintainers)
Commits related to this issue
- fixed the post delete route. It's apparently a bug with sequelize itself: https://github.com/sequelize/sequelize/issues/8444 — committed to JEC6789/tech-blog by JEC6789 2 years ago
I had the same issue. I was missing
hooks: true,
. Adding it solved the issue. Recommend trying the below:I was able to find some insight here: http://docs.sequelizejs.com/manual/tutorial/hooks.html#associations
Ofcourse its still an issue…
The problem is missing
CONSTRAINT
in schema. So, the solution is add a migration script to add foreign key field into table:Here’s my answer on SO: Sequelize.js onDelete: ‘cascade’ is not deleting records sequelize
Hope this help!
Same issue. Solved by changing this
to
No sure how this is solving is
This bug still exists
Two points to note:
This worked for me
I had the same issue, fixed it with changing the order of associations declaration. The
hasMany
needs to be declared before thebelongsTo
.This problem is old but still today this is what solved it for me. If anyone knows a simpler way let me know.
this solves my issue. thanks
Banging my head on the same issue. Basically I have items and outfits. I want to have many items that can possibly belong to many outfits. When I delete an item, I want to also delete every associated outfit. Ended up like this:
Look-up then destroy + the hooks:true did the trick. It’s not optimal, I don’t like 2 calls for one deletion but I think it does work.
@lumpov it’s not the same bug. Your tables are paranoid. Deleting something from a paranoid table updates its
deleted_at
column. It’s not a delete statement so the ON DELETE clause doesn’t applyThis is the issue you’re looking for: https://github.com/sequelize/sequelize/issues/2586
We’ll double check before closing but I’m pretty sure this issue is resolved in Sequelize 7
Not sure how does it solving, but it works. Thanks!
This helped me!