我有一個多對多關(guān)聯(lián)(它用于返回 JSON)。它在模型中聲明:// models/school.gotype School struct { ID int `gorm:"primary_key"` Name string `gorm:"not null"` Accreditations []Accreditation `gorm:"many2many:school_accreditation;"` }效果很好。我在 json 中返回了關(guān)聯(lián)。問題是我的school_accreditation表中有一個附加字段,但它未包含在響應中。我試圖為該協(xié)會聲明一個模型,就像這個答案中提出的那樣:// models/schoolAccreditation.gopackage modelsimport "time"// many to manytype SchoolAccreditation struct { StartedAt time.Time `gorm:"not null"`}但到目前為止還不起作用。是否需要聲明一些額外的配置?還是要修改?
1 回答

滄海一幻覺
TA貢獻1824條經(jīng)驗 獲得超5個贊
回答自己,我將鏈接模型中的字段添加為“忽略”,并且它有效,會自動從關(guān)聯(lián)表中檢索該列。
type Accreditation struct {
// "accreditation" table
ID int `gorm:"primary_key"`
Name string
Description string
// "school_accreditation table", so the field is set as ignore with "-"
EndAt time.Time `gorm:"-"`
}
- 1 回答
- 0 關(guān)注
- 158 瀏覽
添加回答
舉報
0/150
提交
取消