GORM Playground Link
https://github.com/go-gorm/playground/pull/697
Description
I would like to hard delete if the flag is true, as in the code below, but the second Delete statement does not work correctly. Why is this happening? What can I do to avoid this?
code
if tc.shouldHardDelete {
tx = tx.Unscoped()
}
// delete pets separately to delete pets' association
if err := tx.Select(clause.Associations).Delete(&user.Pets).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
if err := tx.Select(clause.Associations).Delete(&user).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
Comment From: a631807682
https://gorm.io/docs/method_chaining.html#Reusability-and-Safety
tx = tx.Unscoped().Session(&gorm.Session{})
Comment From: ksrnnb
Oh, thanks. I had missed this document.
A critical aspect of GORM is understanding when a gorm.DB instance is safe to reuse. Following a Chain Method or Finisher Method, GORM returns an initialized gorm.DB instance. This instance is not safe for reuse as it may carry over conditions from previous operations, potentially leading to contaminated SQL queries.