From 6c5b95e2b262a4379d3bb113117e914be4efe791 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 23 Jun 2014 20:10:50 +0800 Subject: [PATCH] Return query error in FirstOrInit and FirstOrCreate --- main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 17e3246c..8e85f754 100644 --- a/main.go +++ b/main.go @@ -165,7 +165,11 @@ func (s *DB) Scan(dest interface{}) *DB { func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { c := s.clone() - if c.First(out, where...).Error == RecordNotFound { + r := c.First(out, where...) + if r.Error != nil { + if !r.RecordNotFound() { + return r + } c.NewScope(out).inlineCondition(where...).initialize() } else { c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.search.AssignAttrs), false) @@ -175,7 +179,11 @@ func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB { c := s.clone() - if c.First(out, where...).Error == RecordNotFound { + r := c.First(out, where...) + if r.Error != nil { + if !r.RecordNotFound() { + return r + } c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates) } else if len(s.search.AssignAttrs) > 0 { c.NewScope(out).Set("gorm:update_interface", s.search.AssignAttrs).callCallbacks(s.parent.callback.updates)