Gorm的关联删除不会删除行,而是更新行。

huangapple go评论92阅读模式
英文:

Gorm Association Delete does not remove rows, instead update rows

问题

一个客户可以拥有多个角色。我想在删除客户时删除所有的角色。

type Client struct {
    Id        string    `gorm:"primaryKey"`
    CreatedAt time.Time
    UpdatedAt time.Time
    Roles     []Role
}

type Role struct {
    Id        uint      `gorm:"primarykey"`
    CreatedAt time.Time
    UpdatedAt time.Time
    ClientID  string
}

return db.Transaction(func(tx *gorm.DB) error {
    err = db.Model(&clientToRemove).Association("Roles").Delete(&clientToRemove.Roles)
    if err != nil {
        return err
    }

    err = db.Delete(&clientToRemove).Error
    if err != nil {
        return err
    }

    return nil
})

我期望相关的角色行被删除,而不是执行更新查询来删除 client_id。

[210.834ms] [rows:1] UPDATE "role" SET "client_id"=NULL WHERE "role"."client_id" = 'xxxxxxxxxxx' AND "role"."id" = 9

如何完全删除关联的角色表中的行?

数据库是Postgres。

英文:

A client has many Roles. I want to delete all Roles once a client is deleted.

 type Client struct {
    	Id                          string `gorm:"primaryKey"`
    	CreatedAt                   time.Time
    	UpdatedAt                   time.Time
    	Roles [] Role
    }
    
    type Role struct {
    	Id        uint `gorm:"primarykey"`
    	CreatedAt time.Time
    	UpdatedAt time.Time
    
    	ClientID string
    }
    
    return db.Transaction(func(tx *gorm.DB) error {
    		err = db.Model(&clientToRemove).Association("Roles").Delete(&clientToRemove.Roles)
    		if err != nil {
    			return err
    		}
    
    		err = db.Delete(&clientToRemove).Error
    		if err != nil {
    			return err
    		}
    
    		return nil
    	})

I expect related rows in role to be removed, instead of delete query, it executes an update query to remove client_id.

[210.834ms] [rows:1] UPDATE "role" SET "client_id"=NULL WHERE "role"."client_id" = 'xxxxxxxxxxx' AND "role"."id" = 9

How to completely remove rows in associated role table?

Database is Postgres

答案1

得分: 1

根据文档中所述,使用关联删除操作只会删除ClientTenantRole之间的引用关系。在你的情况下,它只是将TenantRole记录更新为将client_id设置为NULL。

如果你想同时删除对象,可以尝试使用带有删除操作的Select。请注意,这仅在主键不为零时有效,所以你的查询可能类似于:

err = db.Select("TenantRoles").Delete(&Client{Id: clientId}).Error

或者如果clientToRemove已经填充了Id字段,可以直接使用它:

err = db.Select("TenantRoles").Delete(&clientToRemove).Error
英文:

As stated in the documentation, the delete with association operation will just remove the references between Client and TenantRole. In your case, it just updated the TenantRole records to set the client_id to NULL.

If you want to delete the objects as well, you can try using Select with the delete operation. Please note that this only works if the primary key is not zero, so your query might look something like this:

err = db.Select("TenantRoles").Delete(&Client{Id: clientId}).Error

or just use clientToRemove if it already has the Id field populated

err = db.Select("TenantRoles").Delete(&clientToRemove).Error

huangapple
  • 本文由 发表于 2022年10月21日 14:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74149407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定