Discussed in https://github.com/go-gorm/gorm/discussions/6344

Originally posted by **pplmx** May 22, 2023 if there is a struct like this:
type Task struct {
    ID       uint
    PrevDeps []*Task `gorm:"many2many:task_depends;joinForeignKey:NextID;joinReferences:PrevID"`
    NextDeps []*Task `gorm:"many2many:task_depends;joinForeignKey:PrevID;joinReferences:NextID"`
}
Using this sentence to delete, which can only delete task(ID=1) and cannot delete NextDeps
var t Task
t.ID = 1
db.Select("NextDeps").Delete(&t).Error

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 30 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: 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 30 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: saeidee

You can delete it using Clear with Unscope function. Gorm how to delete object with self-referenced objects

Comment From: pplmx

    gorm.io/driver/mysql v1.5.0
    gorm.io/gorm v1.25.0
  1. My Association object has no Unscoped func.
  2. I want to soft delete, unscoped clear is a real deletion

Comment From: pplmx

My Association object has no Unscoped func.

Upgrade to gorm 1.25.1, the Unscoped is found.

gorm@v1.25.0#Association, this function is not found in 1.25.0

Comment From: pplmx

I want to soft delete, unscoped clear is a real deletion

According to this doc, it should delete a record with its relations when using select.

Actually, it only works fine on two different objects relationship.

Comment From: pplmx

Hi, @saeidee

I pushed a PR go-gorm/playground/pull/601

Comment From: pplmx

Is it a bug?

The following deleting is SOFT_DELETING.

  • [SUCCESS] A has many B: deleting A will also delete B
  • [ FAILED ] A has many Self-A: deleting A cannot delete Self-A
  • [ FAILED ] A has many B, and B has many C: deleting A will only delete B, it cannot delete C

Comment From: saeidee

Added comment in your PR.

Comment From: pplmx

Summary for the above

Three Scenario

  • A has many B:

    • deleting A, B should be able to be deleted together(/by cascade)
    • A has many Prev-A and many Next-A:

    • deleting A, many Next-A should be able to be deleted together(/by cascade)

    • A has many B, B has many C:

    • deleting A, B and C should be able to be deleted together(/by cascade)

Right Now

It seems that gorm only supports the first case, the remaining two cases are not supported.

Comment From: saeidee

Closing the issue since commented in the playground PR https://github.com/go-gorm/playground/pull/601.

Comment From: pplmx

Can you answer this case:

A has many B, B has many C

Thanks a lot.