[英]gorm many2many and additional fields in association table
I have a many2many association (it is used to return JSON). 我有一个many2many关联(用于返回JSON)。 It's declared in a model:
在模型中声明:
// models/school.go
type School struct {
ID int `gorm:"primary_key"`
Name string `gorm:"not null"`
Accreditations []Accreditation `gorm:"many2many:school_accreditation;"`
}
It works well. 它运作良好。 I have the association returned in the json.
我在json中返回了关联。 The problem is that I have an additional field in the
school_accreditation
table but it isn't included in the response. 问题是我在
school_accreditation
表中还有一个附加字段,但是它没有包含在响应中。
I have tried to declare a model for the association like proposed in this answer : 我已经尝试为这个答案中提出的关联声明一个模型:
// models/schoolAccreditation.go
package models
import "time"
// many to many
type SchoolAccreditation struct {
StartedAt time.Time `gorm:"not null"`
}
But it doesn't work so far. 但这到目前为止还行不通。 Is there some additional configuration to declare?
是否需要声明一些其他配置? Or to modify?
还是要修改?
Answering to myself, I added the field in the linked model as "ignore" and it works, the column is automatically retrieved from the association 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:"-"`
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.