package models import ( "time" "gorm.io/gorm" ) type Album struct { ID uint32 `gorm:"primary_key" json:"id"` Name string `gorm:"not null" json:"name"` Parent *Album `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"parent"` ParentId *uint32 `json:"-"` Author *Account `gorm:"constraint:OnDelete:SET NULL,OnUpdate:CASCADE" json:"author"` AuthorId *uint32 `json:"-"` Photos []*Photo `gorm:"many2many:album_photos" json:"-"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } func (a *Album) AfterFind(tx *gorm.DB) error { if a.ParentId != nil { a.Parent = &Album{} return tx.Preload("Author").First(a.Parent, a.ParentId).Error } return nil }