I deleted the object and want it's ID deleted too in a relationship table. How do I do it? i.e. objects with relations are deleted but the tables of their relations remain.
Also wanted to ask, GORM is the best ORM solution for Go-Gin ?
I deleted the object and want it's ID deleted too in a relationship table. How do I do it? i.e. objects with relations are deleted but the tables of their relations remain.
Also wanted to ask, GORM is the best ORM solution for Go-Gin ?
Try explicitly adding foreign keys and ON DELETE CASCADE
to your models. I've found that GORM can be iffy when it cones to this kind of thing, but doing it explicitly seems to always make it work.
For example:
type Person struct {
ID int
Bio Biography `gorm:"Foreignkey:PersonID;"`
Name string
}
type Biography struct {
ID int
Content string
PersonID int `sql:"type:bigint REFERENCES people(id) ON DELETE CASCADE"`
}
Note: you have to specify what the actual database column and table will be called, not the field. Since GORM automatically pluralizes and converts tables to snake_case, I reference the column people(id)
. If you overwrite this functionality, use whatever table and column name you have.