Skip to content

生成model文件

bash
goctl model mysql ddl --src love-nest.sql --dir .

这种方式时,sql语句似乎有一些要求,在行级别声明的主键,似乎是不生效的,例如:

sql
create table animal_adopt
(
    id               int auto_increment comment '此条信息的id' primary key,
    uid              int            not null comment '用户id',
    animal_id        int            not null comment '动物id',
    publish_time     datetime       not null comment '发起领养时的时间',
    message          varchar(100)   not null comment '发起领养时,填写的信息',
    agree            int default -1 not null,
    animal_master_id int            not null
) charset = utf8;

需要在后面加上,还有不能使用联合主键,这是目前发现的问题

sql
create table animal_adopt
(
    id               int auto_increment comment '此条信息的id',
    uid              int            not null comment '用户id',
    animal_id        int            not null comment '动物id',
    publish_time     datetime       not null comment '发起领养时的时间',
    message          varchar(100)   not null comment '发起领养时,填写的信息',
    agree            int default -1 not null,
    animal_master_id int            not null,
    PRIMARY KEY (`id`)
) charset = utf8;
bash
 goctl model mysql datasource  --url "root:password@tcp(localhost:3306)/love_nest" --table "animals"

直接读取数据库,不需要使用本地的sql,这种更好一点,

生成具体的代码

bash
goctl api go --api .\user.api --dir ../

生成的有些代码,头部会出现// Code generated by goctl. DO NOT EDIT.的标识,请不要修改这些文件