为了账号安全,请及时绑定邮箱和手机立即绑定

gorm Many2many 和关联表中的附加字段

gorm Many2many 和关联表中的附加字段

Go
翻阅古今 2023-07-17 14:48:32
我有一个多对多关联(它用于返回 JSON)。它在模型中声明:// models/school.gotype School struct {    ID                int      `gorm:"primary_key"`    Name              string `gorm:"not null"`    Accreditations    []Accreditation `gorm:"many2many:school_accreditation;"` }效果很好。我在 json 中返回了关联。问题是我的school_accreditation表中有一个附加字段,但它未包含在响应中。我试图为该协会声明一个模型,就像这个答案中提出的那样:// models/schoolAccreditation.gopackage modelsimport "time"// many to manytype SchoolAccreditation struct {    StartedAt time.Time `gorm:"not null"`}但到目前为止还不起作用。是否需要声明一些额外的配置?还是要修改?
查看完整描述

1 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

回答自己,我将链接模型中的字段添加为“忽略”,并且它有效,会自动从关联表中检索该列。


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:"-"`

}


查看完整回答
反对 回复 2023-07-17
  • 1 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信