From 853bd4c280cd8018d78ef424a060b3e73d5072f0 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Tue, 26 Apr 2022 19:07:42 +0800 Subject: [PATCH] fix: duplicate entry in insert transaction --- models/folder.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/models/folder.go b/models/folder.go index 251c127..430c683 100644 --- a/models/folder.go +++ b/models/folder.go @@ -23,15 +23,13 @@ type Folder struct { // Create 创建目录 func (folder *Folder) Create() (uint, error) { - tx := DB.Begin() - if err := tx.FirstOrCreate(folder, *folder).Error; err != nil { - tx.Rollback() - util.Log().Warning("无法插入目录记录, %s", err) - return 0, err + if err := DB.FirstOrCreate(folder, *folder).Error; err != nil { + folder.Model = gorm.Model{} + err2 := DB.First(folder, *folder).Error + return folder.ID, err2 } - err := tx.Commit().Error - return folder.ID, err + return folder.ID, nil } // GetChild 返回folder下名为name的子目录,不存在则返回错误