🐛 fix: delete soft delete error

This commit is contained in:
Martial BE 2024-04-02 12:26:22 +08:00
parent a8891c0f72
commit c80f81b177
No known key found for this signature in database
GPG Key ID: D06C32DF0EDB9084

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"one-api/common" "one-api/common"
"strings" "strings"
"time"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -32,7 +31,7 @@ type User struct {
AffCode string `json:"aff_code" gorm:"type:varchar(32);column:aff_code;uniqueIndex"` AffCode string `json:"aff_code" gorm:"type:varchar(32);column:aff_code;uniqueIndex"`
InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"` InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"`
CreatedTime int64 `json:"created_time" gorm:"bigint"` CreatedTime int64 `json:"created_time" gorm:"bigint"`
DeletedAt *time.Time `gorm:"index"` DeletedAt gorm.DeletedAt `gorm:"index"`
} }
type UserUpdates func(*User) type UserUpdates func(*User)
@ -161,7 +160,15 @@ func (user *User) Delete() error {
if user.Id == 0 { if user.Id == 0 {
return errors.New("id 为空!") return errors.New("id 为空!")
} }
err := DB.Delete(user).Error
// 不改变当前数据库索引,通过更改用户名来删除用户
user.Username = user.Username + "_del_" + common.GetRandomString(6)
err := user.Update(false)
if err != nil {
return err
}
err = DB.Delete(user).Error
return err return err
} }