go-zero
go-zero copied to clipboard
coredump: goctl model mysql ddl --src user_base.sql --dir .
trafficstars
goctl model mysql ddl --src user_base.sql --dir .
[convertColumns]: The column "f_stop_time" is recommended to add constraint `DEFAULT`
[convertColumns]: The column "f_create_time" is recommended to add constraint `DEFAULT`
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x156fd00]
goroutine 1 [running]:
github.com/zeromicro/go-zero/tools/goctl/model/sql/parser.checkDuplicateUniqueIndex(0xc0005dd308?, {0xc000ad0d91, 0xb})
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/parser/parser.go:182 +0x340
github.com/zeromicro/go-zero/tools/goctl/model/sql/parser.Parse({0xc00031b1d0, 0x27}, {0x0, 0x0}, 0x0?)
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/parser/parser.go:162 +0x5c5
github.com/zeromicro/go-zero/tools/goctl/model/sql/gen.(*defaultGenerator).genFromDDL(0x3?, {0xc00031b1d0, 0x27}, 0xd4?, 0xd4?, {0x0, 0x0})
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/gen/gen.go:215 +0x9b
github.com/zeromicro/go-zero/tools/goctl/model/sql/gen.(*defaultGenerator).StartFromDDL(0x7ffddddae538?, {0xc00031b1d0?, 0xc0002409a0?}, 0xc0?, 0xfb?, {0x0?, 0x168dae0?})
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/gen/gen.go:113 +0x28
github.com/zeromicro/go-zero/tools/goctl/model/sql/command.fromDDL({{0x7ffddddae524, 0xd}, {0x7ffddddae538, 0x1}, 0xc0002409a0, 0x0, 0x0, {0x0, 0x0}, 0x0, ...})
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/command/command.go:256 +0x29e
github.com/zeromicro/go-zero/tools/goctl/model/sql/command.MysqlDDL(0xc000004900?, {0x1964c71?, 0x4?, 0x4?})
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/model/sql/command/command.go:96 +0x2e5
github.com/spf13/cobra.(*Command).execute(0xc000004900, {0xc00023dd80, 0x4, 0x4})
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:940 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0xc00044fb00)
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
/root/go/pkg/mod/github.com/spf13/[email protected]/command.go:992
github.com/zeromicro/go-zero/tools/goctl/cmd.Execute()
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/cmd/root.go:46 +0x6d
main.main()
/root/go/pkg/mod/github.com/zeromicro/go-zero/tools/[email protected]/goctl.go:12 +0x4d
# cat user_base.sql
CREATE TABLE `t_user_base` (
`f_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`f_uname` VARCHAR(16),
`f_phone` VARCHAR(20),
`f_email` VARCHAR(32),
`f_stop_time` DATETIME NOT NULL,
`f_create_time` DATETIME NOT NULL,
PRIMARY KEY (`f_id`),
UNIQUE KEY (`f_phone_number`) // error field
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
Crash will not happen with below fix (the diff is on top of latest git repository)
diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go
index a929d97..158a55c 100644
--- a/tools/goctl/model/sql/parser/parser.go
+++ b/tools/goctl/model/sql/parser/parser.go
@@ -149,6 +149,9 @@ func Parse(filename, database string, strict bool) ([]*Table, error) {
for indexName, each := range uniqueKeyMap {
for _, columnName := range each {
+ if fieldM[columnName] == nil {
+ return nil, fmt.Errorf("table %s: unique key with error column name[%s]", e.Name, columnName)
+ }
uniqueIndex[indexName] = append(uniqueIndex[indexName], fieldM[columnName])
}
}
Thanks
Would you please submit a PR?
I can tackle it.
Thanks @POABOB and @kevwan
I will submit PR tonight
Thanks