diff --git a/model/main.go b/model/main.go index b93f01a2..08e3553a 100644 --- a/model/main.go +++ b/model/main.go @@ -235,6 +235,12 @@ func InitLogDB() (err error) { } func migrateDB() error { + // 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录 + if common.UsingMySQL { + // 旧索引可能不存在,忽略删除错误即可 + _ = DB.Exec("ALTER TABLE models DROP INDEX uk_model_name;").Error + _ = DB.Exec("ALTER TABLE vendors DROP INDEX uk_vendor_name;").Error + } if !common.UsingPostgreSQL { return migrateDBFast() } @@ -264,6 +270,12 @@ func migrateDB() error { } func migrateDBFast() error { + // 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录 + if common.UsingMySQL { + _ = DB.Exec("ALTER TABLE models DROP INDEX uk_model_name;").Error + _ = DB.Exec("ALTER TABLE vendors DROP INDEX uk_vendor_name;").Error + } + var wg sync.WaitGroup migrations := []struct { diff --git a/model/model_meta.go b/model/model_meta.go index 5ccd80c5..53b00f28 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -36,7 +36,7 @@ type BoundChannel struct { type Model struct { Id int `json:"id"` - ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name,where:deleted_at IS NULL"` + ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name,priority:1"` Description string `json:"description,omitempty" gorm:"type:text"` Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"` VendorID int `json:"vendor_id,omitempty" gorm:"index"` @@ -44,7 +44,7 @@ type Model struct { Status int `json:"status" gorm:"default:1"` CreatedTime int64 `json:"created_time" gorm:"bigint"` UpdatedTime int64 `json:"updated_time" gorm:"bigint"` - DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name,priority:2"` BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"` EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"` diff --git a/model/vendor_meta.go b/model/vendor_meta.go index fd316156..b96b1d5c 100644 --- a/model/vendor_meta.go +++ b/model/vendor_meta.go @@ -14,13 +14,13 @@ import ( type Vendor struct { Id int `json:"id"` - Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name,where:deleted_at IS NULL"` + Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name,priority:1"` Description string `json:"description,omitempty" gorm:"type:text"` Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"` Status int `json:"status" gorm:"default:1"` CreatedTime int64 `json:"created_time" gorm:"bigint"` UpdatedTime int64 `json:"updated_time" gorm:"bigint"` - DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_vendor_name,priority:2"` } // Insert 创建新的供应商记录