关联 gorm many2many 和附加字段 table

gorm many2many and additional fields in association table

我有一个many2many关联(它被用来return JSON)。它在模型中声明:

// models/school.go
type School struct {
    ID                int      `gorm:"primary_key"`
    Name              string `gorm:"not null"`
    Accreditations    []Accreditation `gorm:"many2many:school_accreditation;"` 
}

效果很好。我在 json 中有关联 returned。问题是我在 school_accreditation table 中有一个附加字段,但它 没有 包含在响应中。

我已尝试为协会声明模型,如 中所提议:

// models/schoolAccreditation.go
package models

import "time"

// many to many
type SchoolAccreditation struct {
    StartedAt time.Time `gorm:"not null"`
}

但目前还行不通。是否有一些额外的配置要声明?或者修改?

回答我自己,我在链接模型中添加了字段 "ignore" 并且有效,该列自动从关联 table 中检索。

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