Add detailed error information when missing table name

This commit is contained in:
Jinzhu 2020-09-29 15:42:58 +08:00
parent a2faa41cbe
commit dbc6b34dce
2 changed files with 7 additions and 3 deletions

View File

@ -83,9 +83,13 @@ func (p *processor) Execute(db *DB) {
if stmt.Model != nil { if stmt.Model != nil {
if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.SQL.Len() == 0)) { if err := stmt.Parse(stmt.Model); err != nil && (!errors.Is(err, schema.ErrUnsupportedDataType) || (stmt.Table == "" && stmt.SQL.Len() == 0)) {
if errors.Is(err, schema.ErrUnsupportedDataType) && stmt.Table == "" {
db.AddError(fmt.Errorf("%w: Table not set, please set it like: db.Model(&user) or db.Table(\"users\")", err))
} else {
db.AddError(err) db.AddError(err)
} }
} }
}
if stmt.Dest != nil { if stmt.Dest != nil {
stmt.ReflectValue = reflect.ValueOf(stmt.Dest) stmt.ReflectValue = reflect.ValueOf(stmt.Dest)

View File

@ -7,10 +7,10 @@ require (
github.com/jinzhu/now v1.1.1 github.com/jinzhu/now v1.1.1
github.com/lib/pq v1.6.0 github.com/lib/pq v1.6.0
gorm.io/driver/mysql v1.0.1 gorm.io/driver/mysql v1.0.1
gorm.io/driver/postgres v1.0.1 gorm.io/driver/postgres v1.0.2
gorm.io/driver/sqlite v1.1.3 gorm.io/driver/sqlite v1.1.3
gorm.io/driver/sqlserver v1.0.4 gorm.io/driver/sqlserver v1.0.4
gorm.io/gorm v1.20.1 gorm.io/gorm v1.20.2
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../