casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
674 stars 206 forks source link

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

Closed chenxi2015 closed 1 month ago

chenxi2015 commented 1 month ago

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 commented 1 month ago

hello ? Can anybody solve it ?

hsluoyz commented 1 month ago

@chenxi2015 plz make a PR to fix it

chenxi2015 commented 1 month ago

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

hsluoyz commented 1 month ago

@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