diff --git a/db/category.go b/db/category.go index 87a341f..76435a2 100644 --- a/db/category.go +++ b/db/category.go @@ -22,12 +22,14 @@ var ( ) func (c *Category) BeforeSave(tx *gorm.DB) error { - var parent Category - if err := tx.Find(&parent, Category{ParentID: c.ParentID, UserID: c.UserID}).Error; err != nil { - return err - } - if parent.ID == 0 { - return ERROR_CATEGORY_PARENT_NOT_FOUND + if c.ParentID != 0 { + var parent Category + if err := tx.Find(&parent, Category{ParentID: c.ParentID, UserID: c.UserID}).Error; err != nil { + return err + } + if parent.ID == 0 { + return ERROR_CATEGORY_PARENT_NOT_FOUND + } } var dup Category if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {