mirror of https://github.com/go-gorm/gorm.git
Fix table name in singular mode in some cases
This commit is contained in:
parent
1558522aaa
commit
6633f325b8
|
@ -50,6 +50,19 @@ type ModelStruct struct {
|
||||||
|
|
||||||
// TableName get model's table name
|
// TableName get model's table name
|
||||||
func (s *ModelStruct) TableName(db *DB) string {
|
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)
|
return DefaultTableNameHandler(db, s.defaultTableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,17 +154,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
|
|
||||||
modelStruct.ModelType = reflectType
|
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
|
// Get all fields
|
||||||
for i := 0; i < reflectType.NumField(); i++ {
|
for i := 0; i < reflectType.NumField(); i++ {
|
||||||
if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
|
if fieldStruct := reflectType.Field(i); ast.IsExported(fieldStruct.Name) {
|
||||||
|
|
Loading…
Reference in New Issue