1. 模型定义

复杂的模型定义不是必须的,此功能用作数据库数据转换和自动建表

默认的表名规则,使用驼峰转蛇形:

除了开头的大写字母以外,遇到大写会增加 ,原名称中的下划线保留。

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  5. func (u *User) TableName() string {
  6. return "auth_user"
  7. }

如果前缀设置为 prefix_ 那么表名为:prefix_auth_user

1.2. 自定义索引

为单个或多个字段增加索引

  1. type User struct {
  2. Id int
  3. Name string
  4. Email string
  5. }
  6. // 多字段索引
  7. func (u *User) TableIndex() [][]string {
  8. return [][]string{
  9. []string{"Id", "Name"},
  10. }
  11. }
  12. // 多字段唯一键
  13. func (u *User) TableUnique() [][]string {
  14. return [][]string{
  15. []string{"Name", "Email"},
  16. }

仅支持 MySQL

默认使用的引擎,为当前数据库的默认引擎,这个是由你的 mysql 配置参数决定的。

你可以在模型里设置 TableEngine 函数,指定使用的引擎

  1. type User struct {
  2. Id int
  3. Name string
  4. Email string
  5. }
  6. // 设置引擎为 INNODB
  7. func (u *User) TableEngine() string {
  8. return "INNODB"
  9. }

1.4. 设置参数

  1. orm:"null;rel(fk)"

多个设置间使用 ; 分隔,设置的值如果是多个,使用 , 分隔。

忽略字段

设置 - 即可忽略 struct 中的字段

  1. type User struct {
  2. ...
  3. AnyField string `orm:"-"`
  4. ...

auto

当 Field 类型为 int, int32, int64, uint, uint32, uint64 时,可以设置字段为自增健

  • 当模型定义里没有主键时,符合上述类型且名称为 Id 的 Field 将被视为自增健。

鉴于 go 目前的设计,即使使用了 uint64,但你也不能存储到他的最大值。依然会作为 int64 处理。

参见 issue

pk

设置为主键,适用于自定义其他类型为主键

null

数据库表默认为 NOT NULL,设置 null 代表 ALLOW NULL

  1. Name string `orm:"null"`

index

unique

为单个字段增加 unique 键

  1. Name string `orm:"unique"`

column

为字段设置 db 字段的名称

  1. Name string `orm:"column(user_name)"`

size

string 类型字段默认为 varchar(255)

设置 size 以后,db type 将使用 varchar(size)

digits / decimals

设置 float32, float64 类型的浮点精度

  1. Money float64 `orm:"digits(12);decimals(4)"`

总长度 12 小数点后 4 位 eg: 99999999.9999

auto_now / auto_now_add

  1. Created time.Time `orm:"auto_now_add;type(datetime)"`
  2. Updated time.Time `orm:"auto_now;type(datetime)"`
  • auto_now 每次 model 保存时都会对时间自动更新
  • auto_now_add 第一次保存时才设置时间

对于批量的 update 此设置是不生效的

type

设置为 date 时,time.Time 字段的对应 db 类型使用 date

  1. Created time.Time `orm:"auto_now_add;type(date)"`

设置为 datetime 时,time.Time 字段的对应 db 类型使用 datetime

  1. Created time.Time `orm:"auto_now_add;type(datetime)"`

default

为字段设置默认值,类型必须符合(目前仅用于级联删除时的默认值)

  1. type User struct {
  2. ...
  3. Status int `orm:"default(1)"`
  4. ...
  5. }

Comment

为字段添加注释

  1. type User struct {
  2. ...
  3. Status int `orm:"default(1)" description:"这是状态字段"`
  4. ...
  5. }

注意: 注释中禁止包含引号

rel / reverse

RelOneToOne:

  1. type User struct {
  2. ...
  3. Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
  4. ...
  5. }

对应的反向关系 RelReverseOne:

  1. type Profile struct {
  2. ...
  3. User *User `orm:"reverse(one)"`
  4. ...
  5. }

RelForeignKey:

  1. type User struct {
  2. ...
  3. Posts []*Post `orm:"reverse(many)"` // fk 的反向关系
  4. ...

RelManyToMany:

  1. type Post struct {
  2. ...
  3. Tags []*Tag `orm:"rel(m2m)"` // ManyToMany relation
  4. ...
  5. }

对应的反向关系 RelReverseMany:

  1. type Tag struct {
  2. ...
  3. Posts []*Post `orm:"reverse(many)"`
  4. ...

rel_table / rel_through

此设置针对 orm:"rel(m2m)" 的关系字段

  1. rel_table 设置自动生成的 m2m 关系表的名称
  2. rel_through 如果要在 m2m 关系中使用自定义的 m2m 关系表
  3. 通过这个设置其名称,格式为 pkg.path.ModelName
  4. eg: app.models.PostTagRel
  5. PostTagRel 表需要有到 Post Tag 的关系

当设置 rel_table 时会忽略 rel_through

设置方法:

orm:"rel(m2m);rel_table(the_table_name)"

orm:"rel(m2m);rel_through(pkg.path.ModelName)"

on_delete

设置对应的 rel 关系删除时,如何处理关系字段。

  1. cascade 级联删除(默认值)
  2. set_null 设置为 NULL,需要设置 null = true
  3. set_default 设置为默认值,需要设置 default
  4. do_nothing 什么也不做,忽略
  1. type User struct {
  2. ...
  3. Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
  4. ...
  5. }
  6. type Profile struct {
  7. ...
  8. User *User `orm:"reverse(one)"`
  9. ...
  10. }
  11. // 删除 Profile 时将设置 User.Profile 的数据库字段为 NULL

关于 on_delete 的相关例子

  1. type User struct {
  2. Id int
  3. Name string
  4. }
  5. type Post struct {
  6. Id int
  7. Title string
  8. User *User `orm:"rel(fk)"`
  9. }

假设 Post -> User 是 ManyToOne 的关系,也就是外键。

  1. o.Filter("Id", 1).Delete()

这个时候即会删除 Id 为 1 的 User 也会删除其发布的 Post

不想删除的话,需要设置 set_null

那这个时候,删除 User 只会把对应的 Post.user_id 设置为 NULL

当然有时候为了高性能的需要,多存点数据无所谓啊,造成批量删除才是问题。

  1. type Post struct {
  2. Id int
  3. Title string
  4. User *User `orm:"rel(fk);null;on_delete(do_nothing)"`
  5. }

那么只要删除的时候,不操作 Post 就可以了。

1.6. 模型字段与数据库类型的对应

在此列出 ORM 推荐的对应数据库类型,自动建表功能也会以此为标准。

MySQL

Sqlite3

PostgreSQL

其字段类型取决于对应的主键。

  • RelForeignKey
  • RelOneToOne
  • RelManyToMany
  • RelReverseOne
  • RelReverseMany