处理 GORM 0 的bug

This commit is contained in:
haomingming
2026-09-08 10:55:15 +08:00
parent b74a8aa898
commit 86f3cc2fbc
11 changed files with 192 additions and 9 deletions
+22
View File
@@ -24,6 +24,28 @@ func (r *DoctorPharmacyDao) GetDoctorPharmacyListByDoctorId(doctorId int64) (m [
if err != nil {
return nil, err
}
// GORM 在外键为 0 时会自动跳过 Preload,因此针对未能预加载的记录进行兜底补全
var zeroPharmacy *model.Pharmacy
for _, dp := range m {
if dp.Pharmacy == nil {
if dp.PharmacyId == 0 {
if zeroPharmacy == nil {
var ph model.Pharmacy
if err := global.Db.Where("pharmacy_id = 0").First(&ph).Error; err == nil {
zeroPharmacy = &ph
}
}
dp.Pharmacy = zeroPharmacy
} else {
var ph model.Pharmacy
if err := global.Db.Where("pharmacy_id = ?", dp.PharmacyId).First(&ph).Error; err == nil {
dp.Pharmacy = &ph
}
}
}
}
return m, nil
}