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

table names cannot be specified after automatic migration is disabled #162

Closed
OldSmokeGun opened this issue May 13, 2022 · 13 comments · Fixed by #163 or #170
Closed

table names cannot be specified after automatic migration is disabled #162

OldSmokeGun opened this issue May 13, 2022 · 13 comments · Fixed by #163 or #170

Comments

@OldSmokeGun
Copy link

#160

This commit can indeed turn off the automatic migration, but it cannot specify a custom data table name

I try to make a commit, but I don't have the environment for unit testing, so I can't pass the test

Hope the NewAdapterWithoutAutoMigrate function can specify the data table name

@casbin-bot casbin-bot added the bug label May 13, 2022
@casbin-bot
Copy link
Member

@tangyang9464 @closetool @sagilio

@hsluoyz
Copy link
Member

hsluoyz commented May 13, 2022

@tangyang9464

@hsluoyz hsluoyz added enhancement and removed bug labels May 13, 2022
@tangyang9464
Copy link
Member

tangyang9464 commented May 13, 2022

@OldSmokeGun Because the adapter gives the user the right to create the table, customize table name and even table structure will be based on the functions provided by gorm itself. You can customize the table name like this

db, _ := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
db.Table("custom_name").Migrator().AutoMigrate(&gormadapter.CasbinRule{})
a, _ := gormadapter.NewAdapterWithoutAutoMigrate(db)

@OldSmokeGun
Copy link
Author

@tangyang9464 Thanks for your answer, but don't you think this limits user only can use the gormadapter.CasbinRule type?

Why can't the user's own structure and table name be passed in like the NewAdapterByDBWithCustomTable function? This is extremely scalable for users

@tangyang9464
Copy link
Member

@tangyang9464 Thanks for your answer, but don't you think this limits user only can use the gormadapter.CasbinRule type?

Why can't the user's own structure and table name be passed in like the NewAdapterByDBWithCustomTable function? This is extremely scalable for users

@OldSmokeGun db.Table("custom_name").Migrator().AutoMigrate(custom_table{}) can define both table name and table structure. Actually NewAdapterByDBWithCustomTable also does it like this. CustomTable just change the gorm struct tags, but the table structure must stay the same. see https://github.com/casbin/gorm-adapter#customize-table-columns-example

@OldSmokeGun
Copy link
Author

@tangyang9464

image

As you can see, the NewAdapterWithoutAutoMigrate function uses defaultTableName, and its value is casbin_rule, which means that even if I use a custom structure when migrating, its table name must be casbin_rule, otherwise, how does the adapter know to use which table

If there are multiple casbins in a system, this will create conflicts

@tangyang9464
Copy link
Member

@OldSmokeGun Got it. Fix later.

@OldSmokeGun
Copy link
Author

@tangyang9464 Thanks for your contribution!

@Lee-0x00
Copy link

Lee-0x00 commented Jun 28, 2022

use TurnOffAutoMigrate is Fail, As follows

db, err := gorm.Open(mysql.Open("root:ddd@tcp(127.0.0.1:3306)/test"), &gorm.Config{})      
gormadapter.TurnOffAutoMigrate(db)       
a, err := gormadapter.NewAdapterByDBWithCustomTable(db, nil, "casbin_rule")

someone like me?

CREATE TABLE `dsc_casbin_rule` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'key',
  `ptype` varchar(100) DEFAULT NULL,
  `v0` varchar(100) DEFAULT NULL,
  `v1` varchar(100) DEFAULT NULL,
  `v2` varchar(100) DEFAULT NULL,
  `v3` varchar(100) DEFAULT NULL,
  `v4` varchar(100) DEFAULT NULL,
  `v5` varchar(100) DEFAULT NULL,
  `v6` varchar(25) DEFAULT NULL,
  `v7` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_casbin_rule` (`ptype`,`v0`,`v1`,`v2`,`v3`),
  UNIQUE KEY `idx_dsc_casbin_rule` (`ptype`,`v0`,`v1`,`v2`,`v3`,`v4`,`v5`,`v6`,`v7`)   # it is automatically generated
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='casbin rule';

@tangyang9464
Copy link
Member

use TurnOffAutoMigrate is Fail, As follows

db, err := gorm.Open(mysql.Open("root:ddd@tcp(127.0.0.1:3306)/test"), &gorm.Config{})      
gormadapter.TurnOffAutoMigrate(db)       
a, err := gormadapter.NewAdapterByDBWithCustomTable(db, nil, "casbin_rule")

someone like me?

@Lee-0x00 db = gormadapter.TurnOffAutoMigrate(db)

@hsluoyz
Copy link
Member

hsluoyz commented Jun 28, 2022

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?

@tangyang9464
Copy link
Member

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?

Check it out later

@tangyang9464
Copy link
Member

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment