From baf6b5ca9fb6446f5bbe49a2b20862e90d897e73 Mon Sep 17 00:00:00 2001 From: Trae AI Date: Wed, 11 Nov 2026 22:10:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=95=E5=88=97=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=94=B9=E7=94=A8=20unique=20=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=EF=BC=8C=E4=BF=AE=E5=A4=8D=E8=BF=81=E7=A7=BB=E5=9B=A0?= =?UTF-8?q?=20DROP=20uni=5F=20=E7=B4=A2=E5=BC=95=E6=8A=A5=201091=20?= =?UTF-8?q?=E4=B8=AD=E6=96=AD=E5=AF=BC=E8=87=B4=E5=BB=BA=E8=A1=A8=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 缺表时直接 CreateTable,避免列级差异中断整表迁移 - packages.currency 手动迁移改用 HasColumn 判断,修正 TEXT 默认值在 MySQL 下非法 --- backend/internal/database/database.go | 24 ++++++++++------------- backend/internal/model/models.go | 28 +++++++++++++-------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/backend/internal/database/database.go b/backend/internal/database/database.go index f7d256d..c7a3f4d 100644 --- a/backend/internal/database/database.go +++ b/backend/internal/database/database.go @@ -243,7 +243,14 @@ func runMigrations() { &model.CloudVariableRecord{}, } for _, m := range models { - if err := DB.AutoMigrate(m); err != nil { + var err error + if DB.Migrator().HasTable(m) { + err = DB.AutoMigrate(m) + } else { + // 表不存在时直接建表,避免个别列级差异中断整表迁移 + err = DB.Migrator().CreateTable(m) + } + if err != nil { log.Printf("Failed to migrate %T: %v\n", m, err) } } @@ -318,20 +325,9 @@ func runMigrations() { } // 手动迁移:检查并添加 packages 表的 currency 列 - var packageColumns []struct { - Name string - } - DB.Raw("PRAGMA table_info(packages)").Scan(&packageColumns) - hasCurrency := false - for _, col := range packageColumns { - if col.Name == "currency" { - hasCurrency = true - break - } - } - if !hasCurrency { + if !DB.Migrator().HasColumn(&model.Package{}, "currency") { log.Println("Adding currency column to packages table...") - if err := DB.Exec("ALTER TABLE packages ADD COLUMN currency TEXT DEFAULT 'CNY'").Error; err != nil { + if err := DB.Exec("ALTER TABLE packages ADD COLUMN currency VARCHAR(10) DEFAULT 'CNY'").Error; err != nil { log.Println("Failed to add currency column:", err.Error()) } else { log.Println("currency column added successfully") diff --git a/backend/internal/model/models.go b/backend/internal/model/models.go index 2de2550..45da442 100644 --- a/backend/internal/model/models.go +++ b/backend/internal/model/models.go @@ -9,8 +9,8 @@ import ( // User 平台用户模型(管理员、代理商) type User struct { ID uint `gorm:"primaryKey" json:"id"` - Username string `gorm:"uniqueIndex;size:50" json:"username"` - Email *string `gorm:"uniqueIndex;size:100" json:"email"` + Username string `gorm:"unique;size:50" json:"username"` + Email *string `gorm:"unique;size:100" json:"email"` Password string `gorm:"size:255" json:"-"` Avatar string `gorm:"size:255" json:"avatar"` Signature string `gorm:"size:255" json:"signature"` @@ -135,7 +135,7 @@ type DynamicCode struct { ID uint `gorm:"primaryKey" json:"id"` ApplicationID uint `gorm:"index;not null" json:"application_id"` Name string `gorm:"size:100;not null" json:"name"` - Key string `gorm:"size:100;uniqueIndex;not null" json:"key"` + Key string `gorm:"size:100;unique;not null" json:"key"` Code string `gorm:"type:longtext;not null" json:"code"` Description string `gorm:"type:text" json:"description"` Status string `gorm:"size:20;default:active" json:"status"` @@ -168,7 +168,7 @@ type Application struct { Name string `gorm:"size:100" json:"name"` Description string `gorm:"type:text" json:"description"` IconURL string `gorm:"size:255" json:"icon_url"` - AppKey string `gorm:"uniqueIndex;size:50" json:"app_key"` + AppKey string `gorm:"unique;size:50" json:"app_key"` BillingType string `gorm:"size:20" json:"billing_type"` LoginPolicy string `gorm:"size:20;default:loose" json:"login_policy"` EncryptType string `gorm:"size:20" json:"encrypt_type"` @@ -299,7 +299,7 @@ type Card struct { ID uint `gorm:"primaryKey" json:"id"` ApplicationID uint `gorm:"index;not null" json:"application_id"` CardTypeID uint `gorm:"index;not null" json:"card_type_id"` - CardKey string `gorm:"uniqueIndex;size:100" json:"card_key"` + CardKey string `gorm:"unique;size:100" json:"card_key"` CreatorID uint `gorm:"index" json:"creator_id"` AgentID *uint `gorm:"index" json:"agent_id"` AppUserID *uint `gorm:"index" json:"app_user_id"` @@ -336,7 +336,7 @@ type Announcement struct { // Order 订单模型 type Order struct { ID uint `gorm:"primaryKey" json:"id"` - OrderNo string `gorm:"uniqueIndex;size:100" json:"order_no"` + OrderNo string `gorm:"unique;size:100" json:"order_no"` UserID uint `gorm:"index" json:"user_id"` ApplicationID *uint `gorm:"index" json:"application_id"` PackageID *uint `json:"package_id"` @@ -391,7 +391,7 @@ type Log struct { type RechargeRecord struct { ID uint `gorm:"primaryKey" json:"id"` UserID uint `gorm:"index" json:"user_id"` - OrderNo string `gorm:"size:50;uniqueIndex" json:"order_no"` + OrderNo string `gorm:"size:50;unique" json:"order_no"` CardID *uint `json:"card_id"` CardCode string `gorm:"size:100" json:"card_code"` Amount float64 `gorm:"type:decimal(10,2)" json:"amount"` @@ -411,7 +411,7 @@ type ConsumptionRecord struct { ID uint `gorm:"primaryKey" json:"id"` UserID uint `gorm:"index" json:"user_id"` ApplicationID uint `gorm:"index" json:"application_id"` - OrderNo string `gorm:"size:50;uniqueIndex" json:"order_no"` + OrderNo string `gorm:"size:50;unique" json:"order_no"` Type string `gorm:"size:50" json:"type"` // verification, card_purchase, subscription, feature, manual_deduct Content string `gorm:"type:text" json:"content"` Description string `gorm:"type:text" json:"description"` @@ -610,7 +610,7 @@ type DocCategory struct { ID uint `gorm:"primaryKey" json:"id"` Name string `gorm:"size:100" json:"name"` NameEn string `gorm:"size:100" json:"name_en"` - Slug string `gorm:"size:100;uniqueIndex" json:"slug"` + Slug string `gorm:"size:100;unique" json:"slug"` Description string `gorm:"size:255" json:"description"` DescriptionEn string `gorm:"size:255" json:"description_en"` Icon string `gorm:"size:50;default:file-text" json:"icon"` @@ -715,7 +715,7 @@ type Doc struct { CategoryID *uint `json:"category_id"` Title string `gorm:"size:200" json:"title"` TitleEn string `gorm:"size:200" json:"title_en"` - Slug string `gorm:"size:200;uniqueIndex" json:"slug"` + Slug string `gorm:"size:200;unique" json:"slug"` Content string `gorm:"type:longtext" json:"content"` ContentEn string `gorm:"type:longtext" json:"content_en"` Summary string `gorm:"size:500" json:"summary"` @@ -737,7 +737,7 @@ type Doc struct { type Setting struct { ID uint `gorm:"primaryKey" json:"id"` Category string `gorm:"size:50;index" json:"category"` // basic, email, sms, payment - Key string `gorm:"size:100;uniqueIndex" json:"key"` + Key string `gorm:"size:100;unique" json:"key"` Value string `gorm:"type:text" json:"value"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` @@ -746,7 +746,7 @@ type Setting struct { // Captcha 验证码模型 type Captcha struct { ID uint `gorm:"primaryKey" json:"id"` - CaptchaID string `gorm:"uniqueIndex;size:50" json:"captcha_id"` + CaptchaID string `gorm:"unique;size:50" json:"captcha_id"` Code string `gorm:"size:10" json:"code"` ExpiresAt time.Time `json:"expires_at"` CreatedAt time.Time `json:"created_at"` @@ -776,7 +776,7 @@ type SmsVerifyCode struct { type AppSMTPConfig struct { ID uint `gorm:"primaryKey" json:"id"` - ApplicationID uint `gorm:"uniqueIndex" json:"application_id"` + ApplicationID uint `gorm:"unique" json:"application_id"` Host string `gorm:"size:100" json:"host"` Port int `gorm:"default:465" json:"port"` User string `gorm:"size:100" json:"user"` @@ -930,7 +930,7 @@ type ExtensionAPIKey struct { ID uint `gorm:"primaryKey" json:"id"` ApplicationID uint `gorm:"index;not null" json:"application_id"` Name string `gorm:"size:100;not null" json:"name"` - AccessKey string `gorm:"uniqueIndex;size:64;not null" json:"access_key"` + AccessKey string `gorm:"unique;size:64;not null" json:"access_key"` SecretKey string `gorm:"size:64;not null" json:"secret_key"` Permissions string `gorm:"type:text" json:"permissions"` // JSON数组存储权限列表 Status string `gorm:"size:20;default:active" json:"status"`