From 7b8e91377b043e76874a0a381bda55635d1289ac Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 26 Jan 2014 21:23:53 +0800 Subject: [PATCH] callback create.go --- callback_create.go | 10 ++++++---- gorm_test.go | 6 ++++-- main.go | 3 +-- private.go | 14 +++++++------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/callback_create.go b/callback_create.go index 00345b3d..fec5fe43 100644 --- a/callback_create.go +++ b/callback_create.go @@ -15,15 +15,17 @@ func Create(scope *Scope) { defer scope.Trace(time.Now()) if !scope.HasError() { + scope.SetColumn("CreatedAt", time.Now()) + scope.SetColumn("UpdatedAt", time.Now()) + // set create sql var sqls, columns []string for _, field := range scope.Fields() { - if field.IsBlank || len(field.SqlTag) == 0 { - continue + if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored { + columns = append(columns, scope.quote(field.DBName)) + sqls = append(sqls, scope.AddToVars(field.Value)) } - columns = append(columns, scope.quote(field.DBName)) - sqls = append(sqls, scope.AddToVars(field.Value)) } scope.Raw(fmt.Sprintf( diff --git a/gorm_test.go b/gorm_test.go index 1a533d20..78bf1d3b 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -22,10 +22,10 @@ type IgnoredEmbedStruct struct { } type User struct { - Id int64 // Id: Primary key - Birthday time.Time // Time + Id int64 // Id: Primary key Age int64 Name string `sql:"size:255"` + Birthday time.Time // Time CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more @@ -189,6 +189,7 @@ func TestPrecision(t *testing.T) { var u User db.First(&u, "name = ?", "Precision") + if u.Latitude != f { t.Errorf("Float64 should not be changed after query") } @@ -868,6 +869,7 @@ func TestUpdate(t *testing.T) { product1 := Product{Code: "123"} product2 := Product{Code: "234"} db.Save(&product1).Save(&product2).Update("code", "456") + if product2.Code != "456" { t.Errorf("Record should be updated with update attributes") } diff --git a/main.go b/main.go index 8f41c44a..bee12997 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,6 @@ func (s *DB) First(out interface{}, where ...interface{}) *DB { func (s *DB) Last(out interface{}, where ...interface{}) *DB { return s.clone().do(out).where(where...).last().db } - func (s *DB) Find(out interface{}, where ...interface{}) *DB { return s.clone().do(out).where(where...).query().db } @@ -180,7 +179,7 @@ func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) * func (s *DB) Save(value interface{}) *DB { scope := s.clone().newScope(value) if scope.PrimaryKeyZero() { - return scope.callCallbacks(s.parent.callback.creates).db + return scope.callCallbacks(s.parent.callback.creates).db.do(value).db } else { return s.clone().do(value).begin().save().commit_or_rollback().db } diff --git a/private.go b/private.go index 1a7bcb2c..4990bdbd 100644 --- a/private.go +++ b/private.go @@ -32,15 +32,15 @@ func (s *DB) do(data interface{}) *Do { func (s *DB) err(err error) error { if err != nil { - if s.logMode == 0 { - if err != RecordNotFound { + if err != RecordNotFound { + if s.logMode == 0 { go s.print(fileWithLineNum(), err) - if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) { - return nil - } + } else { + s.log(err) + } + if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) { + return nil } - } else { - s.log(err) } s.Error = err }