我在GORM中遇到一对多关联的问题。我有这两个结构,我想得到一个病人的完整病史。这是我的示例代码:
type Patient struct {
gorm.Model
Prenom string `json:"prenom" gorm:"column:patient_prenom"`
Nom string `json:"nom" gorm:"column:patient_nom"`
Genre string `json:"genre" gorm:"column:patient_genre"`
Naissance string `json:"naissance" gorm:"column:patient_naissance"`
Historique []Historique `gorm:"ForeignKey:Fk_patient_id"`
}
type Historique struct {
Fk_patient_id string
Date_consultation string
Fk_maladie_id uint
Fk_compte_medecin_id uint
Patient Patient
}
func GetPatientWithDiseases(id uint) (*Patient, error) {
patient := &Patient{}
//The line right there works so i can retrieve without the history
//err := GetDB().Find(patient, id).Error
db := GetDB().Preload("tt_historique").Find(patient)
err := db.Error
if err != nil {
return nil, err
}
return patient, nil
}
其中“Historique”使用患者的外键(Fk_patient_id),而Historique []Historique是查询后应最终出现在Patient结构中的每个Historique的列表。
但是我得到这个错误。我已经尝试了多种语法,这些语法是我在结构中的gorm规范中发现的,但没有任何效果。我只使用GO开发了3天,GORM是我的第一个ORM,所以也许我错过了一些非常明显的东西。can't preload field tt_historique for models.Patient
HUH函数
相关分类