未通过beego保存的API创建数据

时间:2019-04-24 11:54:24

标签: mysql api go frameworks beego

版本:

bee version
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0

├── Beego     : 1.11.1
├── GoVersion : go1.12.1

创建一个beego项目:

bee new quickstart

使用beego的bee工具生成后支架:

bee generate scaffold post -fields="title:string,body:text" -driver=mysql -conn="root:root@tcp(127.0.0.1:3306)/quickstart"

routers / router.go

package routers

import (
    "quickstart/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/posts" ,&controllers.PostController{}, "*:GetAll")
    beego.Router("/posts/add" ,&controllers.PostController{}, "*:Post")
}

在db中初始化一条记录:

mysql> use quickstart;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------------+
| Tables_in_quickstart |
+----------------------+
| migrations           |
| post                 |
+----------------------+
2 rows in set (0.00 sec)

mysql> desc post;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| title | varchar(128) | NO   |     | NULL    |                |
| body  | longtext     | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into post(id, title, body) values(1, 'a', 'b');
Query OK, 1 row affected (0.00 sec)

通过API创建第二条记录:

enter image description here

但是请检查数据列表,titlebody未保存。为什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

也许bee new quickstart没有使用API​​生成结构。这是形式。