Gorm cascade delete does not trigger associated items hook

Viewed 142

Model structure:

type Comment struct {
    BaseModel
    Description string `json:"description" gorm:"type:text"`
    PinID       uint64 `json:"pin_id" gorm:"not null"`
    Files       []File `gorm:"constraint:OnDelete:CASCADE"`
}
type File struct {
    BaseModel
    Name      string `json:"name,omitempty" gorm:"type:text"`
    Type      string `json:"type,omitempty" gorm:"type:varchar(50)"`
    SizeByte  uint64 `json:"size_byte,omitempty"`
    DataURL   string `json:"data_url" gorm:"type:text"`
    CommentID uint64 `json:"comment_id" gorm:"not null"`
}

func (f *File) BeforeDelete(ct *gorm.DB) (err error) {
    fmt.Println("BeforeDelete File:", f, f.Name)
    return
}

If I delete a comment, it deletes all the associated files, but it doesn't trigger the BeforeDelete hook.

I am using this code to delete a Comment:

tx.Delete(&Comment{}, 14)

Can anybody tell me, am I doing any mistake to trigger associated item hooks?

Update: It seems a bug of GORM, so I raised an issue here https://github.com/go-gorm/gorm/issues/5001

0 Answers
Related