gorm
ddl 语句如下:
CREATE TABLE `pf_station_info` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '站点名称',
  `location` json NOT NULL COMMENT '站点所在经纬度',
  `ip_address` varchar(15) NOT NULL DEFAULT '' COMMENT '站点 ip 地址,非此 IP 数据不接收',
  `tiger_shaped` char(32) NOT NULL DEFAULT '' COMMENT '握手符号',
  `heartbeat` int unsigned NOT NULL DEFAULT '0' COMMENT '上次心跳时间戳',
  `status` int NOT NULL DEFAULT '0' COMMENT '站点状态:0 正常,非 0 为停用时间戳',
  `add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
要生成 如下 gorm struct , not null 以及 comment 也在 tag 里 且 location 类型 是 datatypes.JSON 类型
type Station struct {
	Id          int64          `json:"id" gorm:"primaryKey;autoIncrement;comment:主键 id"`
	Name        string         `gorm:"type:varchar(20);column:name;not null;comment:站点名称" json:"name"`
	Location    datatypes.JSON `gorm:"type:json;column:location;not null;comment:站点所在经纬度" json:"location"`
	IpAddress   string         `gorm:"type:varchar(15);column:ip_address;not null;comment:站点 ip 地址,非此 IP 数据不接收" json:"ip_address"`
	TigerShaped string         `gorm:"type:char(32);column:tiger_shaped;not null;comment:握手符号" json:"tiger_shaped"`
	Heartbeat   int64          `gorm:"type:int(10);column:heartbeat;not null;comment:上次心跳时间戳" json:"heartbeat"`
	Status      int64          `gorm:"type:int(11);column:status;comment:站点状态:0 正常,非 0 为停用时间戳" json:"status"`
	AddTime     time.Time      `gorm:"type:datetime;comment:创建时间" json:"add_time,omitempty"`
	UpdateTime  time.Time      `gorm:"type:datetime;comment:修改时间" json:"update_time,omitempty"`
}
有人知道 上面的 struct 是用 什么工具生成的吗?我猜不可能手写吧。
谷歌搜了下 看了好几个 根据 ddl 生成都不一样 ,比如下面的
https://www.qetool.com/sql_json_go/sql.html
utools 里的 sql2struct 生成也不一样
有没大佬推荐个好用点的 或 跟这个 生成一模一样的。
|  |      1treblex      2022-09-14 08:27:18 +08:00 orm 不是從 struct 生成 sql 嗎,應該是手寫的吧 | 
|  |      2guanhui07 OP @treblex #1 不能吧  先写 struct 再 根据 struct 生成 ddl 语句 吧。 那我这个 struct 怎么 生成 ddl 语句 ,用什么工具 转。 我现在想到是 ddl2struct 网上找的 不太一样 只能改改 ,因为好奇所以找了找 ,没找到 一样的。 说不定别人自己写的转换工具 ,哈哈 | 
|  |      3ikaros      2022-09-14 08:49:34 +08:00 @guanhui07 gorm v2 的 migrator interface 里面有个 create table 函数, v1 我记得就是在*gorm.DB 里面, 可以用这个函数创建表 | 
|  |      4idoubi      2022-09-14 08:56:04 +08:00 via Android 可能用我这个工具生成完,然后自己改了一些 tag ? https://dou.tools/sql2struct/? | 
|  |      5to2false      2022-09-14 08:59:02 +08:00 推荐官方下的一个工具 https://github.com/go-gorm/gen | 
|  |      6Johngodme      2022-09-14 09:04:03 +08:00  1 | 
|      7aeli      2022-09-14 09:06:46 +08:00 copilot 可以根据 sql 语句注释之类的自动生成 | 
|  |      8Trim21      2022-09-14 09:12:16 +08:00 应该是 gorm/gen ,具体字段生成出来的类型是可以自己设置的。 | 
|      9xioxu      2022-09-14 09:28:31 +08:00 历史悠久的工具 codeSmith 就可以根据 schema 生成任何代码 | 
|  |      10king888      2022-09-14 09:56:29 +08:00  2 不用 gorm 一般用 fraenky8/tables-to-go 生成 struct ,配合不到 500 行自己封装的 CURD 直接撸,至于 Database Design ,AutoMigrate 是直接用的 DbSchema 这款软件,爽的一批 | 
|  |      11FrankFang128      2022-09-14 10:02:06 +08:00 @king888 看起来不错 | 
|  |      12waltcow      2022-09-14 10:03:02 +08:00 结合 sql ,copilot 一直 tab 到底 | 
|  |      13keepeye      2022-09-14 10:25:43 +08:00 我都是手写 tag ,用 gorm 的 migrate 创建表 | 
|  |      14ElmerZhang      2022-09-14 11:06:58 +08:00 copilot 就可以,把 DDL 以注释的形式粘在文件开头,然后开始写 struct ,写个一两行之后,后面的就都能用 copilot 补全一路 tab 出来。搞完了再把 DDL 注释删了就行了。 | 
|  |      15dalang      2022-09-14 13:59:01 +08:00 | 
|  |      16lrvy      2022-09-14 14:16:13 +08:00 gorm  有个 gen 库 https://github.com/go-gorm/gen | 
|  |      17timethinker      2022-09-15 12:04:45 +08:00 挺有意思,刚才用 JS 手写了一个 DDL 的解析器,可以将这种 CREATE TABLE 转换成 JSON ,然后有了 JSON 以后就可以用来生成代码了,仓促之下只做了 CREATE TABLE 这种 SQL 语句,写的也比较凌乱,可能还有一些 BUG ,不过可以自行拓展。 地址: https://jsfiddle.net/AlexMaho/b6c1utoe/ |