Does gorm have soft cascade delete function? I read Gorm's documentation and tried several methods, but none was successful. I don't know if there is a problem with my code or gorm does not have soft cascade delete function. My code is as follows:

type Entity struct { EntityId uint gorm:"column:entity_id;primary_key" json:"entity_id" EntityName string gorm:"column:entity_name" json:"entity_name" DeletedAt gorm.DeletedAt gorm:"column:deleted_at;null" Attributes []Attribute gorm:"foreignKey:EntityId;references:EntityId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" }

type Attribute struct { AttributeId uint gorm:"column:attribute_id;primary_key" json:"attribute_id" AttributeName string gorm:"column:attribute_name" json:"attribute_name" EntityId uint gorm:"column:entity_id" json:"entity_id" EntityName string gorm:"column:entity_name" json:"entity_name" DeletedAt gorm.DeletedAt gorm:"column:deleted_at;null" }

func EntityDelete(id uint) (bool,error){ err := databases.Db.Delete(&Entity{},id).Error return true,err }

When I executed the EntityDelete method, the delete_at value of the table Entity was successfully stored, but the corresponding data in the table Attribute was not modified.

Could someone help me with this problem? Thanks a lot!!!

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

https://gorm.io/docs/associations.html#Delete-with-Select

Comment From: manpreetsgarha

@michealchen730 has the solution mentioned by @jinzhu worked for you?

Comment From: therealpaulgg

This solution doesn't work for nested associations as far as I can tell - can this be added?

Comment From: tidbeck

I could not get Delete-with-select to work when using Delete-with-primary-key, but setting the primary key in the model worked for me.

databases.Db.Select("Attribute").Delete(&Entity{EntityId: id})

Comment From: admarko

@manpreetsgarha @therealpaulgg @tidbeck @jinzhu

we're trying to do something like:

databases.Db.Select("Attribute").Delete(&Entity{}, "id in (?)", ids)

And it's only (soft) deleting multiple Entity objects but not deleting each of their associated Attribute objects. The Note section of the Delete-with-select docs linked above make it seem like for this to work you need to supply an ID for the .Select() cascade to work - can you not do this if passing in multiple IDs like in the example?

Do we just need to delete the attributes associated with each entity separately?

Comment From: dablelv

Is there anyone can help me. I tried Delete-with-Select but it doesn't worked for me.

My gorm version as below:

gorm.io/driver/mysql v1.4.7
gorm.io/gorm v1.25.2

My model definition as below:

type Model struct {
    ID        uint           `json:"id" gorm:"primarykey"`
    CreatedAt time.Time      `json:"createdAt"`
    UpdatedAt time.Time      `json:"updatedAt"`
    DeletedAt gorm.DeletedAt `json:"-" gorm:"index" `
    CreatorID string         `json:"creatorID"`
    UpdaterID string         `json:"updaterID"`
}

type Attribute struct {
    Model
    Name string `json:"name"`
    Desc string `json:"desc"`
    Values []AttributeValue `json:"values"`
}

type AttributeValue struct {
    Model
    AttributeID uint `json:"attributeID"`
    Value string `json:"value"`
}

Then I using the Delete-with-Select to softly delete the Attribute by ID, and the Attribute was softly deleted successfully, but the associated AttributeValue didn't be soft deleted.

func DelAttribute(id uint) error {
    return Db.Select("Values").Delete(&Attribute{}, id).Error
}

Am I using it in a wrong way?