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

Use the getFullTableName method in NewFilteredAdapterByDB to concatenate data tables with underscores #239

Closed
chenxi2015 opened this issue Jul 12, 2024 · 4 comments · Fixed by #241
Assignees

Comments

@chenxi2015
Copy link
Contributor

My code:

prefix := "cn_"
tableName := "casbin_rule"
adapter, _ := gormadapter.NewFilteredAdapterByDB(db, prefix, tableName)

getFullTableName code:

func (a *Adapter) getFullTableName() string {
	if a.tablePrefix != "" {
		return a.tablePrefix + "_" + a.tableName
	}
	return a.tableName
}

My mysql connect code:

d, err := database.New(
		dbContainer(masterDsn),
		&gorm.Config{
			NamingStrategy: schema.NamingStrategy{
				TablePrefix:   prefix, // set prefix to cn_  is correct,but cn  is wrong in this
				SingularTable: true,
			},
			Logger: newLogger,
		},
		database.WithMaxIdleConn(vars.Config.GetInt("database.mysql.minIdleConn")),
		database.WithMaxOpenConn(vars.Config.GetInt("database.mysql.maxOpenConn")),
		database.WithConnMaxIdleTime(vars.Config.GetDuration("database.mysql.maxIdleTime")*time.Second),
		database.WithConnMaxLifetime(vars.Config.GetDuration("database.mysql.maxLifetime")*time.Minute),
	)

This parameter cannot be shared with the prefix configured in gorm
If I set prefix to cn_, the table name of gorm concatenation will be determined cn_casbin_rule, but the table name of casbin is cncasbin_rule. Why do I need to concatenate with underscores like this? How can I fix it?

@chenxi2015
Copy link
Contributor Author

hello ? Can anybody solve it ?

@hsluoyz
Copy link
Member

hsluoyz commented Jul 16, 2024

@chenxi2015 plz make a PR to fix it

@chenxi2015
Copy link
Contributor Author

#240 I have already make a PR ,plz check it @hsluoyz

@hsluoyz
Copy link
Member

hsluoyz commented Jul 18, 2024

@chenxi2015 the default behavior should not be modified. You are bringing trouble to all old users and give them incompatibility. Fix the problem by yourself in your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants