fix(be): 编辑档案补写到家日期/生日;关迁移期外键修复重复索引

- UpdatePet 的 fields map 之前漏了 birthday 和 arrived_at,
  导致编辑档案改到家日期不落库(首页天数一直显示「-」)
- DisableForeignKeyConstraintWhenMigrating:外键列自动索引和字段
  index tag 撞成同名索引,父子表同迁时报 1061,关掉库级外键约束创建

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-30 17:57:05 +08:00
parent e2ce56680a
commit de2b72221c
2 changed files with 16 additions and 0 deletions
+4
View File
@@ -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 {
+12
View File
@@ -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
}