表结构
type User struct {
UUID string json:"uuid" gorm:"not null;unique"
Username string json:"username" gorm:"not null;unique"
RealName string json:"real_name" gorm:"not null;"
Email string json:"email"
Phone string json:"phone"
Groups []*AtlasGroup json:"groups" gorm:"many2many:atlas_user_group"
}
type Group struct {
UUID string json:"uuid" gorm:"not null;unique"
Name string json:"name" gorm:"not null;uniqueIndex:idx_name_dept"
Dept string json:"dept" gorm:"uniqueIndex:idx_name_dept"
Users []*AtlasUser json:"users" gorm:"many2many:atlas_user_group"
}
问题
我可以通过 db.DB.Select(clause.Associations).Delete(&User{Username: "test"}) 级联删除 test 用户关联的 group 数据 但是没有办法通过 db.DB.Select(clause.Associations).Where("username in ?", []string{"test", "test1"}).Delete(&User{})
debug 提示执行的 sql 语句
DELETE FROM users WHERE username in ('test', 'test1')
报错提示
Cannot delete or update a parent row: a foreign key constraint fails
从 debug sql 以及报错提示来看,像是没有删除用户的 group 表数据就直接去删除用户导致的
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 ✨