diff --git a/model_struct.go b/model_struct.go index 7060d3af..6022cf74 100644 --- a/model_struct.go +++ b/model_struct.go @@ -50,6 +50,19 @@ type ModelStruct struct { // TableName get model's table name func (s *ModelStruct) TableName(db *DB) string { + if s.defaultTableName == "" && db != nil && s.ModelType != nil { + // Set default table name + if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok { + s.defaultTableName = tabler.TableName() + } else { + tableName := ToDBName(s.ModelType.Name()) + if db == nil || !db.parent.singularTable { + tableName = inflection.Plural(tableName) + } + s.defaultTableName = tableName + } + } + return DefaultTableNameHandler(db, s.defaultTableName) } @@ -141,17 +154,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct { modelStruct.ModelType = reflectType - // Set default table name - if tabler, ok := reflect.New(reflectType).Interface().(tabler); ok { - modelStruct.defaultTableName = tabler.TableName() - } else { - tableName := ToDBName(reflectType.Name()) - if scope.db == nil || !scope.db.parent.singularTable { - tableName = inflection.Plural(tableName) - } - modelStruct.defaultTableName = tableName - } - // Get all fields for i := 0; i < reflectType.NumField(); i++ { if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {