From de2b72221c02c0515d7310c96a4fdedefa9ada31 Mon Sep 17 00:00:00 2001 From: Blizzard Date: Thu, 30 Jul 2026 17:57:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(be):=20=E7=BC=96=E8=BE=91=E6=A1=A3=E6=A1=88?= =?UTF-8?q?=E8=A1=A5=E5=86=99=E5=88=B0=E5=AE=B6=E6=97=A5=E6=9C=9F/?= =?UTF-8?q?=E7=94=9F=E6=97=A5=EF=BC=9B=E5=85=B3=E8=BF=81=E7=A7=BB=E6=9C=9F?= =?UTF-8?q?=E5=A4=96=E9=94=AE=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=A4=8D=E7=B4=A2?= =?UTF-8?q?=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - UpdatePet 的 fields map 之前漏了 birthday 和 arrived_at, 导致编辑档案改到家日期不落库(首页天数一直显示「-」) - DisableForeignKeyConstraintWhenMigrating:外键列自动索引和字段 index tag 撞成同名索引,父子表同迁时报 1061,关掉库级外键约束创建 Co-Authored-By: Claude Opus 5 --- pets-be/internal/database/database.go | 4 ++++ pets-be/internal/handler/pet.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pets-be/internal/database/database.go b/pets-be/internal/database/database.go index 279d83e..1ceb88d 100644 --- a/pets-be/internal/database/database.go +++ b/pets-be/internal/database/database.go @@ -25,6 +25,10 @@ func New(cfg *config.Config) (*gorm.DB, error) { TablePrefix: "sundynix_", SingularTable: false, }, + // 关联在 Go 层管,不靠库级外键。迁移期建外键时 MySQL 会为外键列自动 + // 补一个索引,和字段上的 `index` tag 撞成同名索引,父子表同迁时第二次 + // 建索引报 1061 duplicate。关掉外键约束创建,索引就只由 index tag 建一次 + DisableForeignKeyConstraintWhenMigrating: true, Logger: logger.Default.LogMode(logLevel), }) if err != nil { diff --git a/pets-be/internal/handler/pet.go b/pets-be/internal/handler/pet.go index d32a4f4..6aa8546 100644 --- a/pets-be/internal/handler/pet.go +++ b/pets-be/internal/handler/pet.go @@ -126,6 +126,18 @@ func (h *Handler) UpdatePet(c *gin.Context) { if req.Breed != "" { fields["breed"] = req.Breed } + // 生日和到家日期是日期列,得先解析成 time。之前漏了这两个, + // 导致编辑档案时改到家日期不落库(建档走 toInput 才正常) + if req.Birthday != "" { + if t, err := time.ParseInLocation("2006-01-02", req.Birthday, time.Local); err == nil { + fields["birthday"] = t + } + } + if req.ArrivedAt != "" { + if t, err := time.ParseInLocation("2006-01-02", req.ArrivedAt, time.Local); err == nil && !t.After(time.Now()) { + fields["arrived_at"] = t + } + } if req.AvatarFileID != "" { fields["avatar_file_id"] = req.AvatarFileID }