Describe the feature
This feature would be able to delete an object, its related objects, and its related objects' related objects.
For example, say you have a User model, a Profile model, and a Post model. The relations between the tables would be:
User"has one"Profile.Profile"has many"Post.
As it currently stands, when you delete the User object like so:
...
err := db.Select(clause.Associations).Delete(&models.User{Id: id}).Error
if err != nil {
return errors.new(err.Error())
}
...
only the User and the Profile would be deleted while the Post objects would remain in the database.
This new feature would make it so that when you were to delete the "top-level" model, in this case User, all the model's related objects and their related objects and their related objects, etc, would be deleted.
Motivation
The motivation behind such a feature is that it makes it much easier to manage the database because as of now, to get such functionality to work, you would need to use hooks and loops and eventually if your models are related to many objects, you could end up with double, triple nested hooks and loops.
Related Issues
- https://stackoverflow.com/questions/70833600/golang-gorm-cascade-delete-to-nested-fields
- https://stackoverflow.com/questions/73534284/how-to-delete-related-models-of-a-relation-in-gorm
Comment From: CubicrootXYZ
I'd already be happy if nested Select().Delete() would work. Currently I can not get it running.
e.g. db.Select("Profile", "Profile.Posts").Delete....
Comment From: pavittarsingh315
@CubicrootXYZ Hey so I was actually able to get this to work. I made use of the constraints. I did try using them before when I first created this issue. I was using them wrong. But once I implemented them correctly, they worked just fine.
They also go as deep as they can. For example if model 1 is related to model 2 which is related to model 3, if model 1 is deleted all model 2 objs related to model 1 are deleted and all model 3 objs related to model 2 are deleted.
Comment From: flyingfishzxf
@CubicrootXYZ Hey so I was actually able to get this to work. I made use of the constraints. I did try using them before when I first created this issue. I was using them wrong. But once I implemented them correctly, they worked just fine.
They also go as deep as they can. For example if model 1 is related to model 2 which is related to model 3, if model 1 is deleted all model 2 objs related to model 1 are deleted and all model 3 objs related to model 2 are deleted.
Hi, I also encountered this issue. Simply said, I have a ModelA has one ModelB, and ModelB has many ModelC. I added "gorm:constraint:OnDelete:CASCADE" to ModelB field in ModelA and ModelC field in ModelB. When I delete ModelA record using: db.Select(clauses.Associations).Delete(&modelA), only ModelA record and related ModelB was deleted, ModelC records related to ModelB still there, not deleted. Can you post your simply code to reference? I already searched many many github issues and stackoverflows but cannot find the solutions.
UPDATE: the issue only appear when I use sqlite, it works well when I switch to mysql.