Show error message if not using addressable value for auto migration

This commit is contained in:
Jinzhu 2014-11-25 13:44:14 +08:00
parent 1aaac379ae
commit e9684db42a
2 changed files with 11 additions and 5 deletions

View File

@ -363,7 +363,7 @@ func (s *DB) HasTable(value interface{}) bool {
func (s *DB) AutoMigrate(values ...interface{}) *DB { func (s *DB) AutoMigrate(values ...interface{}) *DB {
db := s.clone() db := s.clone()
for _, value := range values { for _, value := range values {
db = db.NewScope(value).autoMigrate().db db = db.NewScope(value).NeedPtr().autoMigrate().db
} }
return db return db
} }

View File

@ -34,14 +34,20 @@ func (scope *Scope) IndirectValue() reflect.Value {
// NewScope create scope for callbacks, including DB's search information // NewScope create scope for callbacks, including DB's search information
func (db *DB) NewScope(value interface{}) *Scope { 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 db.Value = value
return &Scope{db: db, Search: db.search, 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 // New create a new Scope without search information
func (scope *Scope) New(value interface{}) *Scope { func (scope *Scope) New(value interface{}) *Scope {
return &Scope{db: scope.db, Search: &search{}, Value: value} return &Scope{db: scope.db, Search: &search{}, Value: value}