go-gorm / gorm.io

GORM official site

Home Page:https://gorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG?] TableName Func Not Work?

joykk opened this issue · comments

commented

Document Link

gorm.io/gorm v1.24.3
type ActionCreateSrc string

const (
	ActionCreateSrcSys  ActionCreateSrc = "sys"
	ActionCreateSrcUser ActionCreateSrc = "user"
)

err = db.AutoMigrate(
		models.Action{ActionCreateSrc: models.ActionCreateSrcSys},
		models.Action{ActionCreateSrc: models.ActionCreateSrcUser},
	)
// TableName 会将 Action 的表名重写为 `action_XX`
func (x *Action) TableName() string {
	return ActionTName(x.ActionCreateSrc.S())
}
// ActionTName 会将 Action 的表名重写为 `action_XX`
func ActionTName(x string) string {
	return fmt.Sprintf("action_%s", x)
}

AutoMigrate logs

time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[6.769ms] [rows:-] SELECT DATABASE()"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[13.697ms] [rows:1] SELECT SCHEMA_NAME from Information_schema.SCHEMATA where SCHEMA_NAME LIKE 'gogen%' ORDER BY SCHEMA_NAME='gogen' DESC,SCHEMA_NAME limit 1"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[13.226ms] [rows:-] SELECT count() FROM information_schema.tables WHERE table_schema = 'gogen' AND table_name = 'action_' AND table_type = 'BASE TABLE'"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[6.690ms] [rows:-] SELECT DATABASE()"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[13.583ms] [rows:1] SELECT SCHEMA_NAME from Information_schema.SCHEMATA where SCHEMA_NAME LIKE 'gogen%' ORDER BY SCHEMA_NAME='gogen' DESC,SCHEMA_NAME limit 1"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[7.253ms] [rows:-] SELECT * FROM action_ LIMIT 1"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[13.977ms] [rows:-] SELECT column_name, column_default, is_nullable = 'YES', data_type, character_maximum_length, column_type, column_key, extra, column_comment, numeric_precision, numeric_scale , datetime_precision FROM information_schema.columns WHERE table_schema = 'gogen' AND table_name = 'action_' ORDER BY ORDINAL_POSITION"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[6.646ms] [rows:-] SELECT DATABASE()"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[13.499ms] [rows:1] SELECT SCHEMA_NAME from Information_schema.SCHEMATA where SCHEMA_NAME LIKE 'gogen%' ORDER BY SCHEMA_NAME='gogen' DESC,SCHEMA_NAME limit 1"
time="2023-01-18T18:05:33+08:00" level=info msg="E:/code/golang/chaos_api/internal/store/store.go:143\n[12.825ms] [rows:-] SELECT count(
) FROM information_schema.statistics WHERE table_schema = 'gogen' AND table_name = 'action_' AND index_name = 'idx_action__deleted_at'"

result

image
not find action_sys or action_user, find action_

Expected answer

action_user
action_sys

Not Work Key Code

# gorm.io/gorm@1.24.3/schema/shema.go#func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Namer, specialTableName string) (*Schema, error) {


modelValue := reflect.New(modelType) // Why New?
tableName := namer.TableName(modelType.Name())
// if tabler, ok := dest.(Tabler); ok { // Is it possible to fix like this?
if tabler, ok := modelValue.Interface().(Tabler); ok {
	tableName = tabler.TableName()
}