Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG?] TableName Func Not Work? #630

Open
joykk opened this issue Jan 18, 2023 · 0 comments
Open

[BUG?] TableName Func Not Work? #630

joykk opened this issue Jan 18, 2023 · 0 comments
Assignees

Comments

@joykk
Copy link

joykk commented Jan 18, 2023

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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants