From e9684db42a74b8ae3a1485fde5112fc7b908a850 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 25 Nov 2014 13:44:14 +0800 Subject: [PATCH] Show error message if not using addressable value for auto migration --- main.go | 2 +- scope.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ec974cae..af626b06 100644 --- a/main.go +++ b/main.go @@ -363,7 +363,7 @@ func (s *DB) HasTable(value interface{}) bool { func (s *DB) AutoMigrate(values ...interface{}) *DB { db := s.clone() for _, value := range values { - db = db.NewScope(value).autoMigrate().db + db = db.NewScope(value).NeedPtr().autoMigrate().db } return db } diff --git a/scope.go b/scope.go index 0af4dc8e..8c09cedc 100644 --- a/scope.go +++ b/scope.go @@ -34,14 +34,20 @@ func (scope *Scope) IndirectValue() reflect.Value { // NewScope create scope for callbacks, including DB's search information func (db *DB) NewScope(value interface{}) *Scope { - // reflectKind := reflect.ValueOf(value).Kind() - // if !((reflectKind == reflect.Invalid) || (reflectKind == reflect.Ptr)) { - // fmt.Printf("%v %v\n", fileWithLineNum(), "using unaddressable value") - // } db.Value = value return &Scope{db: db, Search: db.search, Value: value} } +func (scope *Scope) NeedPtr() *Scope { + reflectKind := reflect.ValueOf(scope.Value).Kind() + if !((reflectKind == reflect.Invalid) || (reflectKind == reflect.Ptr)) { + err := errors.New(fmt.Sprintf("%v %v\n", fileWithLineNum(), "using unaddressable value")) + scope.Err(err) + fmt.Printf(err.Error()) + } + return scope +} + // New create a new Scope without search information func (scope *Scope) New(value interface{}) *Scope { return &Scope{db: scope.db, Search: &search{}, Value: value}