fix: DB: Submission nested preload

This commit is contained in:
Paul Pan 2023-12-23 15:37:46 +08:00
parent de95e8c804
commit 1a5b8270ef
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
2 changed files with 12 additions and 7 deletions

View File

@ -4,9 +4,10 @@ import "gorm.io/gorm"
type Submission struct { type Submission struct {
gorm.Model `json:"meta"` gorm.Model `json:"meta"`
ProblemID uint `json:"problem_id" gorm:"not null;index"` ProblemID uint `json:"-" gorm:"not null;index"`
UserID uint `json:"-" gorm:"not null;index"` Problem Problem `json:"problem" gorm:"foreignKey:ProblemID"`
User User `json:"user" gorm:"foreignKey:UserID"` UserID uint `json:"-" gorm:"not null;index"`
Language string `json:"language" gorm:"not null"` User User `json:"user" gorm:"foreignKey:UserID"`
Code string `json:"code" gorm:"not null"` Language string `json:"language" gorm:"not null"`
Code string `json:"code" gorm:"not null"`
} }

View File

@ -18,7 +18,10 @@ func (s *service) Query(sid uint, associations bool) (*model.Status, e.Status) {
query := s.db.Get() query := s.db.Get()
if associations { if associations {
query = query.Preload(clause.Associations) query = query.
Preload("Submission.Problem").
Preload("Submission.User").
Preload(clause.Associations)
} }
err := query. err := query.
@ -43,7 +46,8 @@ func (s *service) QueryByVersion(pvid uint, offset int, limit int, count *int64)
IsEnabled: true, IsEnabled: true,
} }
err := s.db.Get().Preload(clause.Associations). err := s.db.Get().
Preload("Submission.Problem").Preload("Submission.User").Preload(clause.Associations).
Where(status). Where(status).
Order("created_at DESC"). Order("created_at DESC").
Offset(offset).Limit(limit).Find(&ret). Offset(offset).Limit(limit).Find(&ret).