GORM Playground Link

https://github.com/go-gorm/playground/pull/425

Model structure with hook

type BaseModel struct {
    ID        uint64    `json:"id" gorm:"primarykey"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}

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
}

Description

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) //14 is the primary key for a comment

[N.B I am using PostgreSQL database]

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: li-jin-gou

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

@arif2009 hello, you can provide a demo in https://github.com/go-gorm/playground?

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

Hi @arif2009

The current CASCADE implementation is relying on db, so it won't trigger hooks method.

Comment From: arif2009

Hi @arif2009

The current CASCADE implementation is relying on db, so it won't trigger hooks method.

@jinzhu Got it. Thanks 🙇‍♂️ It would be better if it supports this type of triggering.