beego ORM 模型定义表的关系

ORM模型定义表的关系

一. 自动生成表规则

未定义其他规则时,自动生表时,所有字段都为NOT NULL,id为自增主键,其他都有其类型默认值

二. 一对一关系

// rel(one) -> 反向关系 reverse(one)

/*
mysql> desc user;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | NO   |     |         |                |
| profile_id | int(11)      | NO   | UNI | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
*/
type User struct {
	Id      int
	Name    string
	Profile *Profile `orm:"rel(one)"`      // OneToOne relation 生成-> 字段 : profile_id key : 唯一建
}

/*
mysql> desc profile;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| age   | smallint(6) | NO   |     | 0       |                |
+-------+-------------+------+-----+---------+----------------+
*/
type Profile struct {
	Id   int
	Age  int16
	User *User `orm:"reverse(one)"` // 设置一对一反向关系(可选)
}

rel(one): 创建对应关系的字段:表_id,即对应主键,有唯一约束
reverse(one): 不会创建字段,可选tag

一对多

// rel(fk) -> 反向关系 reverse(many)
/*
mysql> desc user;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| name      | varchar(255) | NO   |     |         |                |
| nickname  | varchar(255) | NO   |     |         |                |
| mobile    | varchar(255) | NO   |     |         |                |
| age       | int(11)      | NO   |     | 0       |                |
| sex       | tinyint(1)   | NO   |     | 0       |                |
| email     | varchar(255) | NO   |     |         |                |
| address   | varchar(255) | NO   |     |         |                |
| pass      | varchar(255) | NO   |     |         |                |
| addtime   | int(11)      | NO   |     | 0       |                |
| lastlogin | int(11)      | NO   |     | 0       |                |
+-----------+--------------+------+-----+---------+----------------+
 */
// 一个会员有多篇文章
type User struct {
	Id        int        `json:"id"`
	Name      string     `json:"name"`
	Nickname  string     `json:"nickname"`
	Mobile    string     `json:"mobile"`
	Age       int        `json:"age"`
	Sex       bool       `json:"sex"`
	Email     string     `json:"email"`
	Address   string     `json:"address"`
	Pass      string     `json:"pass"`
	Addtime   int        `json:"addtime"`
	Lastlogin int        `json:"lastlogin"`
	Articles  []*Article `orm:"reverse(many)"`
}

/*
mysql> desc article;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| title   | varchar(255) | NO   |     |         |                |
| content | varchar(255) | NO   |     |         |                |
| addtime | int(11)      | NO   |     | 0       |                |
| uptime  | int(11)      | NO   |     | 0       |                |
| user_id | int(11)      | NO   |     | NULL    |                |
| link    | varchar(255) | NO   |     |         |                |
| intro   | varchar(255) | NO   |     |         |                |
+---------+--------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)
 */
type Article struct {
	Id       int          `json:"id"`
	Title    string       `json:"title"`
	Content  string       `json:"content"`
	Addtime  int          `json:"addtime"`
	Uptime   int          `json:"uptime"`
	User     *User        `json:"user" orm:"rel(fk)"`
	Link     string       `json:"link"`
	Intro    string       `json:"intro"`
}

rel(fk): 创建对应关系的字段:表名_id,即对应主键,没有约束
reverse(many): 不会创建字段,写在关系为多的类里

多对多

// rel(m2m) -> 反向关系 reverse(many)
/*
mysql> desc post;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| title   | varchar(255) | NO   |     |         |                |
| user_id | int(11)      | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

 */
type Post struct {
	Id    int
	Title string
	User  *User  `orm:"rel(fk)"` 
	Tags  []*Tag `orm:"rel(m2m)"` // 多对多关系
}
/*
mysql> desc tag;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     |         |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 */
type Tag struct {
	Id    int
	Name  string
	Posts []*Post `orm:"reverse(many)"`
}

rel(m2m): 不会创建字段,声明处的表名,为自动创建的表的表名前缀(post)
reverse(many): 不会创建字段,声明处的表名,为自动创建的表的表名后缀+s(tags)

通过下划线连接: post_tags

声明关系后,会创建一个表:

mysql> desc post_tags;
+---------+------------+------+-----+---------+----------------+
| Field   | Type       | Null | Key | Default | Extra          |
+---------+------------+------+-----+---------+----------------+
| id      | bigint(20) | NO   | PRI | NULL    | auto_increment |
| post_id | int(11)    | NO   |     | NULL    |                |
| tag_id  | int(11)    | NO   |     | NULL    |                |
+---------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
展开阅读全文

没有更多推荐了,返回首页