From 8a7d52efda24578a54843d02c86da2dee31f7c79 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Mon, 11 Nov 2024 22:26:21 +0200 Subject: [PATCH] Fix Parent finding issue with BeforeSave hook and duplicate finding error for category --- db/category.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/category.go b/db/category.go index 2b97e92..6a7e463 100644 --- a/db/category.go +++ b/db/category.go @@ -40,7 +40,7 @@ var ( func (c *Category) BeforeSave(tx *gorm.DB) error { if c.ParentID != 0 { var parent Category - if err := tx.Find(&parent, c.ID).Error; err != nil { + if err := tx.Find(&parent, c.ParentID).Error; err != nil { return err } if parent.ID == 0 { @@ -54,7 +54,7 @@ func (c *Category) BeforeSave(tx *gorm.DB) error { if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil { return err } - if dup.ID != 0 { + if c.ID != dup.ID && dup.ID != 0 { return ERROR_CATEGORY_NAME_NOT_UNIQUE } return nil