在Gorm中,级联删除(On delete cascade)不按预期工作。

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

On delete cascade not working as intended in Gorm

问题

所以我对Gorm非常陌生,正在尝试使用它,但似乎无法使级联删除起作用。这是我的模型:

type Base struct {
    Id        string    `json:"id" gorm:"type:uuid;primary_key"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}

type User struct {
    Base
    Role     string  `json:"role"`
    Username string  `json:"username" gorm:"unique"`
    Password string  `json:"password"`
    Profile  Profile `gorm:"constraint:OnDelete:CASCADE;"`
}

type Profile struct {
    Base
    UserId string `json:"user_id"`
    Name   string `json:"name"`
    Bio    string `json:"bio" gorm:"default:hello world!"`
    Age    uint8  `json:"age"`
}

问题是,当我对用户对象执行删除操作时,它会被正确删除,但关联的Profile对象不会被删除。我知道Gorm有软删除功能,但我的Base模型中没有gorm.DeletedAt字段。我的UserProfile模型也共享相同的Base,所以它们在删除方面应该表现类似。

这是我执行删除的方式:

...
id := "my-uuid" // 这将是一个真实的值。这只是一个示例
Database.Where("id = ?", id).Delete(&models.User{})
...

我做错了什么?

谢谢!

编辑:
我知道这个问题,并尝试按照它的指导进行操作,但似乎无法使其工作。

英文:

So I am very new to Gorm and am playing around with it but I can't seem to get on delete cascade to work. These are my models:

type Base struct {
	Id string `json:"id" gorm:"type:uuid;primary_key"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type User struct {
	Base
	Role     string  `json:"role"`
	Username string  `json:"username" gorm:"unique"`
	Password string  `json:"password"`
	Profile  Profile `gorm:"constraint:OnDelete:CASCADE;"`
}

type Profile struct {
	Base
	UserId string `json:"user_id"`
	Name   string `json:"name"`
	Bio    string `json:"bio" gorm:"default:hello world!"`
	Age    uint8  `json:"age"`
}

The problem is, when I perform a delete operation on a user object, it gets deleted properly but it's associated Profile object isn't deleted. I know that Gorm has a soft delete functionality but I don't have a gorm.DeletedAt field in my Base model. My User and Profile model also share the same Base so they should behave similarly in terms of the delete.

Here's how I am running the delete:

...
id := "my-uuid" // this would be a real value. this is just an example
Database.Where("id = ?", id).Delete(&models.User{})
...

What am I doing wrong?

Thank you!

Edit:
I am aware of this question and have tried following it but I can't seem to get this to work.

答案1

得分: 2

如果你正在使用 gorm v2,你可以尝试使用 delete with select 来删除主对象、它的关联对象以及相关联的对象。代码应该类似于:

Database.Select("Profile").Delete(&models.User{ID: id})
英文:

If you are using gorm v2, you can try deleting the main object, its relation, and the associated objects by using delete with select. It should be something like this:

Database.Select("Profile").Delete(&models.User{ID: id})

答案2

得分: 0

如果你的数据库支持外键,你可以使用外键(foreign key)。如果不支持外键,你需要使用**事务(transaction)**来删除用户和个人资料对象。

> 参考链接:https://gorm.io/docs/transactions.html

英文:

you could use foreign key if your database support foreign key, if not you need use transaction to deleted the user and profile object.

> https://gorm.io/docs/transactions.html

huangapple
  • 本文由 发表于 2022年8月26日 15:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/73497523.html
匿名

发表评论

匿名网友

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

确定