forked from mirror/gorm
Show error message if not using addressable value for auto migration
This commit is contained in:
parent
1aaac379ae
commit
e9684db42a
2
main.go
2
main.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
14
scope.go
14
scope.go
|
@ -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}
|
||||||
|
|
Loading…
Reference in New Issue