Link another struct - ForeignKey #6042
-
I have this structs: environment:
type Submission struct {
GormModel
SubmissionRequest
UserId int `json:"-"`
User User `gorm:"foreignKey:UserId" json:"user"`
PublisherId int `json:"-"`
Publisher Publisher `gorm:"foreignKey:PublisherId" json:"publisher"`
} //@name models.submission
type User struct {
GormModel
UserRequest
Type string `json:"type"` // like: author, publisher
} //@name models.user
type Publisher struct {
GormModel
PublisherRequest
} //@name models.publisher
type GormModel struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
} //@name models.gormModel (I need the gormModel because I need to rename the attributes for JSON as they cannot be used in the frontend by Swagger.) When I try to read a submission, I get an error because the relationship is unsupported. models.Submission{
GormModel: models.GormModel{
ID: uint(submissionId),
}
}
err := dbClient.db.Preload("Users").Preload("Publishers").First(&submission).Error
if err != nil {
return nil, err
}
return &submission, nil Also when I remove all the gorm-annotation from the attributes, recreate the db and tables the same error comes up although it has the same structure as the demo on gorm.io-preload. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If anyone recognizes my problem from above I would still like to know my fault. For all those who have a similar problem, I have my solution here.
|
Beta Was this translation helpful? Give feedback.
If anyone recognizes my problem from above I would still like to know my fault. For all those who have a similar problem, I have my solution here.
gorm.io-preload-all