模型定义

原文:https://beego.wiki/docs/mvc/model/models/

Model Definition 模型定义

Model Definition 模型定义

Model names are used for database data conversion and Database Schema Generation

​ 模型名称用于数据库数据转换和数据库模式生成

Naming conventions 命名约定

Table name conversion consists in translating camel case used for model names to snake case for table names as follows:

​ 表名转换包括将用于模型名称的驼峰式大小写转换为表名的蛇形大小写,如下所示:

AuthUser -> auth_user
Auth_User -> auth__user
DB_AuthUser -> d_b__auth_user

In other words, all is converted to lower case and _ is the separator. Every uppercase add a separator before it, except the first one.

​ 换句话说,所有内容都转换为小写, _ 是分隔符。每个大写字母在其前面添加一个分隔符,第一个除外。

Custom table name 自定义表名

Using TableNameI interface:

​ 使用 TableNameI 接口:

1
2
3
4
5
6
7
8
type User struct {
	Id int
	Name string
}

func (u *User) TableName() string {
	return "auth_user"
}

If you set prefix to prefix_, the table name will be prefix_auth_user.

​ 如果将前缀设置为 prefix_ ,则表名将为 prefix_auth_user

Custom index 自定义索引

Using TableIndexI interface:

​ 使用 TableIndexI 接口:

Add index to one or more fields:

​ 为一个或多个字段添加索引:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type User struct {
	Id    int
	Name  string
	Email string
}

// multiple fields index
func (u *User) TableIndex() [][]string {
	return [][]string{
		[]string{"Id", "Name"},
	}
}

// multiple fields unique key
func (u *User) TableUnique() [][]string {
	return [][]string{
		[]string{"Name", "Email"},
	}
}

Custom engine 自定义引擎

Only supports MySQL database

​ 仅支持 MySQL 数据库

The default engine is the default engine of the current database engine of your mysql settings.

​ 默认引擎是您 mysql 设置的当前数据库引擎的默认引擎。

Using TableEngineI interface:

​ 使用 TableEngineI 接口:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type User struct {
	Id    int
	Name  string
	Email string
}

// Set engine to INNODB
func (u *User) TableEngine() string {
	return "INNODB"
}

Set parameters 设置参数

1
orm:"null;rel(fk)"

Use ; as the separator of multiple settings. Use , as the separator if a setting has multiple values.

​ 使用 ; 作为多个设置的分隔符。如果某个设置具有多个值,请使用 , 作为分隔符。

Ignore field 忽略字段

Use - to ignore field in the struct.

​ 使用 - 忽略结构中的字段。

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

auto 自动

When Field type is int, int32, int64, uint, uint32 or uint64, you can set it as auto increment.

​ 当字段类型为 int、int32、int64、uint、uint32 或 uint64 时,您可以将其设置为自增。

  • If there is no primary key in the model definition, the field Id with one of the types above will be considered as auto increment key 如果模型定义中没有主键,则类型为上述类型之一的字段 Id 将被视为自增键

pk

Set as primary key. Used for using other type field as primary key.

​ 设置为主键。用于使用其他类型字段作为主键。

null

Fields are NOT NULL by default. Set null to ALLOW NULL.

​ 字段默认值为 NOT NULL 。将 null 设置为 ALLOW NULL

1
Name string `orm:"null"`

index 索引

Add index for one field

​ 为一个字段添加索引

unique

Add unique key for one field

​ 为一个字段添加唯一键

1
Name string `orm:"unique"`

column

Set column name in db table for field.

​ 为字段设置数据库表中的列名。

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

size 大小

Default value for string field is varchar(255).

​ 字符串字段的默认值为 varchar(255)。

It will use varchar(size) after setting.

​ 设置后将使用 varchar(size)。

1
Title string `orm:"size(60)"`

digits / decimals 数字/小数

Set precision for float32 or float64.

​ 为 float32 或 float64 设置精度。

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

Total 12 digits, 4 digits after point. For example: 12345678.1234

​ 总共 12 位数字,小数点后 4 位数字。例如: 12345678.1234

auto_now / auto_now_add

1
2
Created time.Time `orm:"auto_now_add;type(datetime)"`
Updated time.Time `orm:"auto_now;type(datetime)"`
  • auto_now: every save will update time. auto_now:每次保存都会更新时间。
  • auto_now_add: set time at the first save auto_now_add:在首次保存时设置时间

This setting won’t affect massive update.

​ 此设置不会影响大规模 update

type 类型

If set type as date, the field’s db type is date.

​ 如果将类型设置为日期,则字段的数据库类型为日期。

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

If set type as datetime, the field’s db type is datetime.

​ 如果将类型设置为日期时间,则字段的数据库类型为日期时间。

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

Time Precision 时间精度

1
2
3
4
5
type User struct {
...
Created time.Time `orm:"type(datetime);precision(4)"`
...
}

default value 默认值

you could use it like:

​ 您可以像这样使用它:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import (
"github.com/beego/beego/v2/client/orm/filter/bean"
"github.com/beego/beego/v2/client/orm"
)

type DefaultValueTestEntity struct {
Id            int
Age           int `default:"12"`
AgeInOldStyle int `orm:"default(13);bee()"`
AgeIgnore     int
}

func XXX() {
    builder := bean.NewDefaultValueFilterChainBuilder(nil, true, true)
    orm.AddGlobalFilterChain(builder.FilterChain)
    o := orm.NewOrm()
    _, _ = o.Insert(&User{
        ID: 1,
        Name: "Tom",
    })
}
NewDefaultValueFilterChainBuilder`will create an instance of `DefaultValueFilterChainBuilder` In beego v1.x, the default value config looks like `orm:default(xxxx)` But the default value in 2.x is `default:xxx`, so if you want to be compatible with v1.x, please pass true as `compatibleWithOldStyle

NewDefaultValueFilterChainBuilder 将创建一个 DefaultValueFilterChainBuilder 的实例。在 beego v1.x 中,默认值配置如下所示 orm:default(xxxx) 。但在 2.x 中,默认值为 default:xxx ,因此如果您想与 v1.x 兼容,请将 true 作为 compatibleWithOldStyle 传递。

Comment 注释

Set comment value for field.

​ 为字段设置注释值。

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

Relationships 关系

One to one 一对一

RelOneToOne:

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

The reverse relationship RelReverseOne:

​ 反向关系 RelReverseOne:

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

One to many 一对多

RelForeignKey:

1
2
3
4
5
type Post struct {
	...
	User *User `orm:"rel(fk)"` // RelForeignKey relation
	...
}

The reverse relationship RelReverseMany:

​ 反向关系 RelReverseMany:

1
2
3
4
5
type User struct {
	...
	Posts []*Post `orm:"reverse(many)"` // reverse relationship of fk
	...
}

Many to many 多对多

RelManyToMany:

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

The reverse relationship RelReverseMany:

​ 反向关系 RelReverseMany:

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

In this example, by default the auto-generated table name is: post_tag. The name of the struct in which we have orm:"rel(m2m)" defines the first half part, the name of the struct in which we have orm:"reverse(many)" defines the other half. It respects the naming conversion convention we have seen in Naming conventions

​ 在此示例中,默认情况下自动生成的表名为: post_tag 。我们拥有 orm:"rel(m2m)" 的结构的名称定义了前半部分,我们拥有 orm:"reverse(many)" 的结构的名称定义了后半部分。它遵循我们在命名约定中看到的命名转换约定

rel_table / rel_through

This setting is for orm:"rel(m2m)" field:

​ 此设置适用于 orm:"rel(m2m)" 字段:

rel_table       Set the auto-generated m2m connecting table name
rel_through     If you want to use custom m2m connecting table, set name by using this setting.
              Format: `project_path/current_package.ModelName`
              For example: `app/models.PostTagRel` PostTagRel table needs to have a relationship to Post table and Tag table.

If rel_table is set, rel_through is ignored.

​ 如果设置了 rel_table,则忽略 rel_through。

You can set these as follows:

​ 您可以按如下方式设置这些内容:

orm:"rel(m2m);rel_table(the_table_name)"
orm:"rel(m2m);rel_through(project_path/current_package.ModelName)"

on_delete

Set how to deal with field if related relationship is deleted:

​ 设置在删除相关关系时如何处理字段:

cascade        cascade delete (default)
set_null       set to NULL. Need to set null = true
set_default    set to default value. Need to set default value.
do_nothing     do nothing. ignore.
type User struct {
	...
	Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
	...
}
type Profile struct {
	...
	User *User `orm:"reverse(one)"`
	...
}

// Set User.Profile to NULL while deleting Profile

Examples of on_delete on_delete 示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type User struct {
    Id int
    Name string
}

type Post struct {
    Id int
    Title string
    User *User `orm:"rel(fk)"`
}

Assume Post -> User is ManyToOne relationship by foreign key.

​ 假设 Post -> User 是通过外键建立的 ManyToOne 关系。

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

This will delete User with Id 1 and all his Posts.

​ 这将删除 Id 为 1 的 User 及其所有 Post。

If you don’t want to delete the Posts, you need to set set_null

​ 如果您不想删除 Post,则需要设置 set_null

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

In this case, only set related Post.user_id to NULL while deleting.

​ 在这种情况下,仅在删除时将相关的 Post.user_id 设置为 NULL。

Usually for performance purposes, it doesn’t matter to have redundant data. The massive deletion is the real problem

​ 通常出于性能目的,拥有冗余数据无关紧要。真正的问题是大规模删除

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

So just don’t change Post (ignore it) while deleting User.

​ 因此,在删除用户时,不要更改帖子(忽略它)。

Model fields mapping with database type 模型字段与数据库类型的映射

Here is the recommended database type mapping. It’s also the standard for table generation.

​ 以下是推荐的数据库类型映射。它也是表生成的标准。

All the fields are NOT NULL by default.

​ 默认情况下,所有字段均为非空。

MySQL

gomysql
int, int32 - set as auto or name is Id int、int32 - 设置为自动或名称为 Idinteger AUTO_INCREMENT
int64 - set as auto or name isId int64 - 设置为自动或名称为 Idbigint AUTO_INCREMENT
uint, uint32 - set as auto or name is Id uint、uint32 - 设置为 auto 或名称为 Idinteger unsigned AUTO_INCREMENT
uint64 - set as auto or name is Id uint64 - 设置为 auto 或名称为 Idbigint unsigned AUTO_INCREMENT
boolbool
string - default size 255 string - 默认大小 255varchar(size)
string - set type(char) string - 设置类型 (char)char(size)
string - set type(text) string - 设置类型 (text)longtext
time.Time - set type as date time.Time - 设置类型为日期date 日期
time.Timedatetime 日期时间
byte 字节tinyint unsigned 无符号小整数
rune 符文integer 整数
intinteger 整数
int8tinyint 小整数
int16smallint 短整数
int32integer 整数
int64bigint 大整数
uintinteger unsigned 无符号整数
uint8tinyint unsigned 无符号小整数
uint16smallint unsigned 无符号短整数
uint32integer unsigned 无符号整数
uint64bigint unsigned 无符号大整数
float32 32 位浮点数double precision 双精度
float64 64 位浮点数double precision 双精度
float64 - set digits and decimals float64 - 设置数字和小数numeric(digits, decimals)

Sqlite3

gosqlite3
int, int32, int64, uint, uint32, uint64 - set as auto or name is Id int, int32, int64, uint, uint32, uint64 - 设置为自动或名称为 Idinteger AUTOINCREMENT
boolbool
string - default size 255 字符串 - 默认大小 255varchar(size)
string - set type(char) 字符串 - 设置类型(char)character(size) 字符(大小)
string - set type(text) 字符串 - 设置类型(文本)text 文本
time.Time - set type as date 时间.时间 - 设置类型为日期date 日期
time.Timedatetime 日期时间
byte 字节tinyint unsigned 无符号小整数
rune 符文integer 整数
intinteger 整数
int8tinyint
int16smallint
int32integer
int64bigint
uintinteger unsigned
uint8tinyint unsigned
uint16smallint unsigned
uint32integer unsigned
uint64bigint unsigned
float32real 实数
float64real 实数
float64 - set digits and decimals float64 - 设置位数与小数位数decimal 小数

PostgreSQL

gopostgres
int, int32, int64, uint, uint32, uint64 - set as auto or name is Id int、int32、int64、uint、uint32、uint64 - 设置为自动或为 Idserial 序列
boolbool
string - if not set size default text 字符串 - 如果未设置为大小,则默认文本varchar(size)
string - set type(char)char(size)
string - set type(text)text
string - set type(json)json
string - set type(jsonb)jsonb
time.Time - set type as datedate 日期
time.Timetimestamp with time zone 带时区的timestamp
byte 字节smallint CHECK(“column” >= 0 AND “column” <= 255)
runeinteger
intinteger
int8smallint CHECK(“column” >= -127 AND “column” <= 128)
int16smallint
int32integer
int64bigint
uintbigint CHECK(“column” >= 0)
uint8smallint CHECK(“column” >= 0 AND “column” <= 255)
uint16integer CHECK(“column” >= 0)
uint32bigint CHECK(“column” >= 0)
uint64bigint CHECK(“column” >= 0)
float32double precision
float64double precision
float64 - set digits and decimals float64 - 设置数字和小数numeric(digits, decimals)

Relational fields 关系字段

It’s field type depends on related primary key. 它的字段类型取决于相关的主键。

  • RelForeignKey
  • RelOneToOne
  • RelManyToMany
  • RelReverseOne
  • RelReverseMany
最后修改 October 10, 2024: 更新 (a4b8f85)