Your issue may already be reported! Please search on the issue track before creating one.
What version of Go are you using (go version)?
go version go1.12.1 linux/amd64
Which database and its version are you using?
postgres:9.6
Please provide a complete runnable program to reproduce your issue. IMPORTANT
I am using this model with a many to many association :
type School struct {
ID int `gorm:"primary_key"`
Accreditations []Accreditation `gorm:"many2many:school_accreditation"`
Name string `gorm:"not null"`
}
// Retrieve all schools from Pg
var schools []models.School
service.DB.
Model(models.School{}).
Preload("Accreditations").
Find(&schools)
It works well. Except that the association table has an additional field, and this one is not returned when retrieving the main School entity. How can I do to include it?
Comment From: COil
Answering to my 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
ID int `gorm:"primary_key"`
Name string
Description string
// school accreditation
EndAt time.Time `gorm:"-"`
}
Comment From: sneko
@COil curious to know how you managed at that time the association table with extra columns? On my side I create a third struct to define additional tables.
Also, if you ignore the EndAt in your example, how do you put data easily when inserting a School linked to a Accreditation?
Thank you,
EDIT: ok it seems https://github.com/go-gorm/gorm.io/commit/654fd7e8b5e2f431b4f104653f483dd83b841c41 could do the trick since the third struct exists in my code
Comment From: COil
Sorry, that's a long time ago.
Comment From: nccapo
@COil curious to know how you managed at that time the association table with extra columns? On my side I create a third struct to define additional tables.
Also, if you ignore the
EndAtin your example, how do you put data easily when inserting aSchoollinked to aAccreditation?Thank you,
EDIT: ok it seems go-gorm/gorm.io@654fd7e could do the trick since the third struct exists in my code
Hi, i just wonder if it's a correct way to define third struct. Thanks
Comment From: COil
That's a long time ago sorry, leaving this issue. Good luck.