復雜的模型定義不是必須的,此功能用作數(shù)據(jù)庫數(shù)據(jù)轉換和自動建表
默認的表名規(guī)則,使用駝峰轉蛇形:
AuthUser -> auth_user
Auth_User -> auth__user
DB_AuthUser -> d_b__auth_user
除了開頭的大寫字母以外,遇到大寫會增加 _,原名稱中的下劃線保留。
type User struct {
Id int
Name string
}
func (u *User) TableName() string {
return "auth_user"
}
如果前綴設置為 prefix_ 那么表名為:prefix_auth_user
為單個或多個字段增加索引
type User struct {
Id int
Name string
Email string
}
// 多字段索引
func (u *User) TableIndex() [][]string {
return [][]string{
[]string{"Id", "Name"},
}
}
// 多字段唯一鍵
func (u *User) TableUnique() [][]string {
return [][]string{
[]string{"Name", "Email"},
}
}
僅支持 MySQL
默認使用的引擎,為當前數(shù)據(jù)庫的默認引擎,這個是由你的 mysql 配置參數(shù)決定的。
你可以在模型里設置 TableEngine 函數(shù),指定使用的引擎
type User struct {
Id int
Name string
Email string
}
// 設置引擎為 INNODB
func (u *User) TableEngine() string {
return "INNODB"
}
orm:"null;rel(fk)"
多個設置間使用 ; 分隔,設置的值如果是多個,使用 , 分隔。
設置 - 即可忽略 struct 中的字段
type User struct {
...
AnyField string `orm:"-"`
...
}
當 Field 類型為 int, int32, int64, uint, uint32, uint64 時,可以設置字段為自增健
鑒于 go 目前的設計,即使使用了 uint64,但你也不能存儲到他的最大值。依然會作為 int64 處理。
參見 issue 6113
設置為主鍵,適用于自定義其他類型為主鍵
數(shù)據(jù)庫表默認為 NOT NULL,設置 null 代表 ALLOW NULL
Name string `orm:"null"`
為單個字段增加索引
為單個字段增加 unique 鍵
Name string `orm:"unique"`
為字段設置 db 字段的名稱
Name string `orm:"column(user_name)"`
string 類型字段默認為 varchar(255)
設置 size 以后,db type 將使用 varchar(size)
Title string `orm:"size(60)"`
設置 float32, float64 類型的浮點精度
Money float64 `orm:"digits(12);decimals(4)"`
總長度 12 小數(shù)點后 4 位 eg: 99999999.9999
Created time.Time `orm:"auto_now_add;type(datetime)"`
Updated time.Time `orm:"auto_now;type(datetime)"`
對于批量的 update 此設置是不生效的
設置為 date 時,time.Time 字段的對應 db 類型使用 date
Created time.Time `orm:"auto_now_add;type(date)"`
設置為 datetime 時,time.Time 字段的對應 db 類型使用 datetime
Created time.Time `orm:"auto_now_add;type(datetime)"`
為字段設置默認值,類型必須符合(目前僅用于級聯(lián)刪除時的默認值)
type User struct {
...
Status int `orm:"default(1)"`
...
}
為字段添加注釋
type User struct {
...
Status int `orm:"default(1)" description:"這是狀態(tài)字段"`
...
}
注意: 注釋中禁止包含引號
RelOneToOne:
type User struct {
...
Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
...
}
對應的反向關系 RelReverseOne:
type Profile struct {
...
User *User `orm:"reverse(one)"`
...
}
RelForeignKey:
type Post struct {
...
User *User `orm:"rel(fk)"` // RelForeignKey relation
...
}
對應的反向關系 RelReverseMany:
type User struct {
...
Posts []*Post `orm:"reverse(many)"` // fk 的反向關系
...
}
RelManyToMany:
type Post struct {
...
Tags []*Tag `orm:"rel(m2m)"` // ManyToMany relation
...
}
對應的反向關系 RelReverseMany:
type Tag struct {
...
Posts []*Post `orm:"reverse(many)"`
...
}
此設置針對 orm:"rel(m2m)" 的關系字段
rel_table 設置自動生成的 m2m 關系表的名稱
rel_through 如果要在 m2m 關系中使用自定義的 m2m 關系表
通過這個設置其名稱,格式為 pkg.path.ModelName
eg: app.models.PostTagRel
PostTagRel 表需要有到 Post 和 Tag 的關系
當設置 rel_table 時會忽略 rel_through
設置方法:
orm:"rel(m2m);rel_table(the_table_name)"
orm:"rel(m2m);rel_through(pkg.path.ModelName)"
設置對應的 rel 關系刪除時,如何處理關系字段。
cascade 級聯(lián)刪除(默認值)
set_null 設置為 NULL,需要設置 null = true
set_default 設置為默認值,需要設置 default 值
do_nothing 什么也不做,忽略
type User struct {
...
Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
...
}
type Profile struct {
...
User *User `orm:"reverse(one)"`
...
}
// 刪除 Profile 時將設置 User.Profile 的數(shù)據(jù)庫字段為 NULL
type User struct {
Id int
Name string
}
type Post struct {
Id int
Title string
User *User `orm:"rel(fk)"`
}
假設 Post -> User 是 ManyToOne 的關系,也就是外鍵。
o.Filter("Id", 1).Delete()
這個時候即會刪除 Id 為 1 的 User 也會刪除其發(fā)布的 Post
不想刪除的話,需要設置 set_null
type Post struct {
Id int
Title string
User *User `orm:"rel(fk);null;on_delete(set_null)"`
}
那這個時候,刪除 User 只會把對應的 Post.user_id 設置為 NULL
當然有時候為了高性能的需要,多存點數(shù)據(jù)無所謂啊,造成批量刪除才是問題。
type Post struct {
Id int
Title string
User *User `orm:"rel(fk);null;on_delete(do_nothing)"`
}
那么只要刪除的時候,不操作 Post 就可以了。
在此列出 ORM 推薦的對應數(shù)據(jù)庫類型,自動建表功能也會以此為標準。
默認所有的字段都是 NOT NULL
go | mysql |
---|---|
int, int32 - 設置 auto 或者名稱為 Id 時 | integer AUTO_INCREMENT |
int64 - 設置 auto 或者名稱為 Id 時 | bigint AUTO_INCREMENT |
uint, uint32 - 設置 auto 或者名稱為 Id 時 | integer unsigned AUTO_INCREMENT |
uint64 - 設置 auto 或者名稱為 Id 時 | bigint unsigned AUTO_INCREMENT |
bool | bool |
string - 默認為 size 255 | varchar(size) |
string - 設置 type(char) 時 | char(size) |
string - 設置 type(text) 時 | longtext |
time.Time - 設置 type 為 date 時 | date |
time.Time | datetime |
byte | tinyint unsigned |
rune | integer |
int | integer |
int8 | tinyint |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | integer unsigned |
uint8 | tinyint unsigned |
uint16 | smallint unsigned |
uint32 | integer unsigned |
uint64 | bigint unsigned |
float32 | double precision |
float64 | double precision |
float64 - 設置 digits, decimals 時 | numeric(digits, decimals) |
go | sqlite3 |
---|---|
int, int32, int64, uint, uint32, uint64 - 設置 auto 或者名稱為 Id 時 | integer AUTOINCREMENT |
bool | bool |
string - 默認為 size 255 | varchar(size) |
string - 設置 type(char) 時 | character(size) |
string - 設置 type(text) 時 | text |
time.Time - 設置 type 為 date 時 | date |
time.Time | datetime |
byte | tinyint unsigned |
rune | integer |
int | integer |
int8 | tinyint |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | integer unsigned |
uint8 | tinyint unsigned |
uint16 | smallint unsigned |
uint32 | integer unsigned |
uint64 | bigint unsigned |
float32 | real |
float64 | real |
float64 - 設置 digits, decimals 時 | decimal |
go | postgres |
---|---|
int, int32, int64, uint, uint32, uint64 - 設置 auto 或者名稱為 Id 時 | serial |
bool | bool |
string - 若沒有指定 size 默認為 text | varchar(size) |
string - 設置 type(char) 時 | char(size) |
string - 設置 type(text) 時 | text |
string - 設置 type(json) 時 | json |
string - 設置 type(jsonb) 時 | jsonb |
time.Time - 設置 type 為 date 時 | date |
time.Time | timestamp with time zone |
byte | smallint CHECK("column" >= 0 AND "column" <= 255) |
rune | integer |
int | integer |
int8 | smallint CHECK("column" >= -127 AND "column" <= 128) |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | bigint CHECK("column" >= 0) |
uint8 | smallint CHECK("column" >= 0 AND "column" <= 255) |
uint16 | integer CHECK("column" >= 0) |
uint32 | bigint CHECK("column" >= 0) |
uint64 | bigint CHECK("column" >= 0) |
float32 | double precision |
float64 | double precision |
float64 - 設置 digits, decimals 時 | numeric(digits, decimals) |
其字段類型取決于對應的主鍵。
更多建議: