gobee generate api 链接信息链接数据库创建项目报错

现象

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会通过字段的宽度来验证字符类型长度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值