let's say I have these 3 struct
type Question struct {
gorm.Model
Id *uint64 `json:"id" gorm:"column=id;primaryKey;autoIncrement"`
Name string `json:"name" gorm:"unique"`
SkillId *uint64 `json:"skill_id"`
TestQuestions []TestQuestion `json:"test_questions"`
}
type Skill struct {
gorm.Model
SkillName string `json:"skill_name"`
Question []Question
}
type TestQuestion struct {
gorm.Model
QuestionId uint64 `json:"question_id"`
TestId uint64 `json:"test_id"`
}
I want to select all questions and for each question i want to select the skill name for that question instead of skill id and i want to preload the TestQuestion i tried to make this struct to store my result
type struct QuestionResponse(
SkillName string
Name string `json:"name"`
TestQuestions TestQuestion `json:"test_questions"`
}
i tried this query
db.Table("questions").Preload("TestQuestions").
Joins("inner join skills on questions.skill_id = skills.id").
Select("questions.name,skills.skill_name, questions.difficulty, questions.max_points, questions.type, questions.expected_time, questions.question_text,questions.file_read_me").
Find(&question)
but i get this error "invalid field found for struct github.com...../models.QuestionResponse's field TestQuestions: define a valid foreign key for relations or implement the Valuer/Scanner interface" any solution ?