[英]gorm many2many and additional fields in association table
我有一個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
表中還有一個附加字段,但是它沒有包含在響應中。
我已經嘗試為這個答案中提出的關聯聲明一個模型:
// models/schoolAccreditation.go
package models
import "time"
// many to many
type SchoolAccreditation struct {
StartedAt time.Time `gorm:"not null"`
}
但這到目前為止還行不通。 是否需要聲明一些其他配置? 還是要修改?
對我自己的回答是,我在鏈接的模型中將字段添加為“忽略”,並且該字段有效,該列是從關聯表中自動檢索的。
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:"-"`
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.