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