I have a function that deletes an entry based on the URI sent to its API:
type BaseModel struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;"`
CreatedAt time.Time
UpdatedAt time.Time
} // there is no DeletedAt, so no soft deletes (see https://stackoverflow.com/questions/67749708/cascade-delete-in-gorm-does-not-remove-associated-tables for a similar question)
type Person struct {
BaseModel
Timezone string `gorm:"default:Etc/UTC"`
Name string
ConfLoadFromBrowser bool
ConfLoadFromDb bool
Pills []Pill `gorm:"constraint:OnDelete:CASCADE;"`
}
type Pill struct {
BaseModel
PersonID string
Name string
Amount int
Posologies []Posology
}
func deletePerson(w http.ResponseWriter, r *http.Request) {
personId := chi.URLParam(r, "personId")
db.Delete(&Person{}, "id = ?", personId)
// removed checks of successful delete and HTTP responses for simplicityt
}
When requesting for instance DELETE /people/3fe88a09-afaf-4a36-95ae-af822add7103, the relevant entry in the table people (Person in go → table people in SQLite) is deleted as expected (I see the record disappearing in the SQLite db), but entries in the table pills that link to it are not.
Is there a specific way to trigger a CASCADE DELETE in such HasMany model?