现象
gobee根据已有的数据库创建项目
bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:<password>@tcp(127.0.0.1:3306)/test"]
报错:
______
| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
\____/ \___| \___| v1.12.0
2021/08/31 18:03:19 INFO ▶ 0001 generate api project support go modules.
2021/08/31 18:03:19 INFO ▶ 0002 Creating API...
create D:\Go_workspace\src\IMApi\IMApi\go.mod
create D:\Go_workspace\src\IMApi\IMApi
create D:\Go_workspace\src\IMApi\IMApi\conf
create D:\Go_workspace\src\IMApi\IMApi\controllers
create D:\Go_workspace\src\IMApi\IMApi\tests
create D:\Go_workspace\src\IMApi\IMApi\conf\app.conf
create D:\Go_workspace\src\IMApi\IMApi\main.go
2021/08/31 18:03:19 INFO ▶ 0003 Using 'mysql' as 'driver'
2021/08/31 18:03:19 INFO ▶ 0004 Using 'root:root@tcp(localhost:3306)/test' as 'conn'
2021/08/31 18:03:19 INFO ▶ 0005 Using 'im_users' as 'tables'
2021/08/31 18:03:19 INFO ▶ 0006 Analyzing database tables...
panic: runtime error: index out of range [2] with length 0
goroutine 1 [running]:
github.com/beego/bee/generate.extractIntSignness(0xc000198e33, 0x3, 0xc000198e28, 0x6)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/generate/g_appcode.go:930 +0xcd
github.com/beego/bee/generate.(*MysqlDB).GetColumns(0x11ea050, 0xc000186410, 0xc0001a6240, 0xc000184ab0)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/generate/g_appcode.go:481 +0xfb5
github.com/beego/bee/generate.getTableObjects(0xc0001807e0, 0x1, 0x1, 0xc000186410, 0xd37700, 0x11ea050, 0x0, 0x1, 0x98)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/generate/g_appcode.go:363 +0x1e9
github.com/beego/bee/generate.gen(0xc0000a20c8, 0x5, 0xc0000ca036, 0x22, 0xc0000c9707, 0xc0000c9658, 0xc0001ae420, 0x1f)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/generate/g_appcode.go:316 +0x2e5
github.com/beego/bee/generate.GenerateAppcode(0xc0000a20c8, 0x5, 0xc0000ca036, 0x22, 0xbf642a, 0x1, 0xc0000a20a8, 0x8, 0xc0001ae420, 0x1f)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/generate/g_appcode.go:295 +0x232
github.com/beego/bee/cmd/commands/api.createAPI(0x1195080, 0xc0000d4020, 0x4, 0x6, 0x0)
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/cmd/commands/api/apiapp.go:647 +0x1932
main.main()
D:/Go_workspace/pkg/mod/github.com/beego/bee@v1.12.3/main.go:61 +0x297
原因
1.MySQL8.0.17版本之后,integer类型字段将不再显示长度
2.gobee 生成modelfile 的时候会验证int 的宽度
if isSQLSignedIntType(dataType) {
sign := extractIntSignness(columnType)
if sign == "unsigned" && extra != "auto_increment" {
col.Type, err = mysqlDB.GetGoDataType(dataType + " " + sign)
if err != nil {
beeLogger.Log.Fatalf("%s", err)
}
}
}
func extractIntSignness(colType string) string {
regex := regexp.MustCompile(`(int|smallint|mediumint|bigint)\([0-9]+\)(.*)`)
signRegex := regex.FindStringSubmatch(colType)
return strings.Trim(signRegex[2], " ")
}
生成modelfile 的时候通过isSQLSignedIntType验证int类型,在extractIntSignness会通过字段的宽度来验证字符类型长度