我有一个many2many关联(用于返回JSON)。在模型中声明:
// models/school.go
type School struct {
ID int `gorm:"primary_key"`
Name string `gorm:"not null"`
Accreditations []Accreditation `gorm:"many2many:school_accreditation;"`
}
效果很好。我在json中返回了关联。问题是我在school_accreditation
表中还有一个附加字段,但未包含在响应中。
我已经尝试为this answer中提出的关联声明一个模型:
// models/schoolAccreditation.go
package models
import "time"
// many to many
type SchoolAccreditation struct {
StartedAt time.Time `gorm:"not null"`
}
但是到目前为止,它还行不通。是否需要声明一些其他配置?还是要修改?
答案 0 :(得分:1)
对我自己来说,我在链接模型中将字段添加为“忽略”,并且该字段有效,该列是从关联表中自动检索的。
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:"-"`
}