英文:
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
根据文档中所述,使用关联删除操作只会删除Client
和TenantRole
之间的引用关系。在你的情况下,它只是将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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论