国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

beego模型定義

2023-11-20 18:07 更新

復(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"
}

設(shè)置參數(shù)

orm:"null;rel(fk)"

多個(gè)設(shè)置間使用 ; 分隔,設(shè)置的值如果是多個(gè),使用 , 分隔。

忽略字段

設(shè)置 - 即可忽略 struct 中的字段

type User struct {
...
    AnyField string `orm:"-"`
...
}

auto

當(dāng) Field 類(lèi)型為 int, int32, int64, uint, uint32, uint64 時(shí),可以設(shè)置字段為自增健

  • 當(dāng)模型定義里沒(méi)有主鍵時(shí),符合上述類(lèi)型且名稱(chēng)為 Id 的 Field 將被視為自增健。

鑒于 go 目前的設(shè)計(jì),即使使用了 uint64,但你也不能存儲(chǔ)到他的最大值。依然會(huì)作為 int64 處理。

參見(jiàn) issue 6113

pk

設(shè)置為主鍵,適用于自定義其他類(lèi)型為主鍵

null

數(shù)據(jù)庫(kù)表默認(rèn)為 NOT NULL,設(shè)置 null 代表 ALLOW NULL

Name string `orm:"null"`

index

為單個(gè)字段增加索引

unique

為單個(gè)字段增加 unique 鍵

Name string `orm:"unique"`

column

為字段設(shè)置 db 字段的名稱(chēng)

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

size

string 類(lèi)型字段默認(rèn)為 varchar(255)

設(shè)置 size 以后,db type 將使用 varchar(size)

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

digits / decimals

設(shè)置 float32, float64 類(lèi)型的浮點(diǎn)精度

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

總長(zhǎng)度 12 小數(shù)點(diǎn)后 4 位 eg: 99999999.9999

auto_now / auto_now_add

Created time.Time `orm:"auto_now_add;type(datetime)"`
Updated time.Time `orm:"auto_now;type(datetime)"`
  • auto_now 每次 model 保存時(shí)都會(huì)對(duì)時(shí)間自動(dòng)更新
  • auto_now_add 第一次保存時(shí)才設(shè)置時(shí)間

對(duì)于批量的 update 此設(shè)置是不生效的

type

設(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)"`

default

為字段設(shè)置默認(rèn)值,類(lèi)型必須符合(目前僅用于級(jí)聯(lián)刪除時(shí)的默認(rèn)值)

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

Comment

為字段添加注釋

type User struct {
    ...
    Status int `orm:"default(1)" description:"這是狀態(tài)字段"`
    ...
}

注意: 注釋中禁止包含引號(hào)

表關(guān)系設(shè)置

rel / reverse

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)"`
    ...
}

rel_table / rel_through

此設(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)"

on_delete

設(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

關(guān)于 on_delete 的相關(guān)例子

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 就可以了。

模型字段與數(shù)據(jù)庫(kù)類(lèi)型的對(duì)應(yīng)

在此列出 ORM 推薦的對(duì)應(yīng)數(shù)據(jù)庫(kù)類(lèi)型,自動(dòng)建表功能也會(huì)以此為標(biāo)準(zhǔn)。

默認(rèn)所有的字段都是 NOT NULL

MySQL

gomysql
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
boolbool
string - 默認(rèn)為 size 255varchar(size)
string - 設(shè)置 type(char) 時(shí)char(size)
string - 設(shè)置 type(text) 時(shí)longtext
time.Time - 設(shè)置 type 為 date 時(shí)date
time.Timedatetime
bytetinyint unsigned
runeinteger
intinteger
int8tinyint
int16smallint
int32integer
int64bigint
uintinteger unsigned
uint8tinyint unsigned
uint16smallint unsigned
uint32integer unsigned
uint64bigint unsigned
float32double precision
float64double precision
float64 - 設(shè)置 digits, decimals 時(shí)numeric(digits, decimals)

Sqlite3

gosqlite3
int, int32, int64, uint, uint32, uint64 - 設(shè)置 auto 或者名稱(chēng)為 Id 時(shí)integer AUTOINCREMENT
boolbool
string - 默認(rèn)為 size 255varchar(size)
string - 設(shè)置 type(char) 時(shí)character(size)
string - 設(shè)置 type(text) 時(shí)text
time.Time - 設(shè)置 type 為 date 時(shí)date
time.Timedatetime
bytetinyint unsigned
runeinteger
intinteger
int8tinyint
int16smallint
int32integer
int64bigint
uintinteger unsigned
uint8tinyint unsigned
uint16smallint unsigned
uint32integer unsigned
uint64bigint unsigned
float32real
float64real
float64 - 設(shè)置 digits, decimals 時(shí)decimal

PostgreSQL

gopostgres
int, int32, int64, uint, uint32, uint64 - 設(shè)置 auto 或者名稱(chēng)為 Id 時(shí)serial
boolbool
string - 若沒(méi)有指定 size 默認(rèn)為 textvarchar(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.Timetimestamp with time zone
bytesmallint 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 - 設(shè)置 digits, decimals 時(shí)numeric(digits, decimals)

關(guān)系型字段

其字段類(lèi)型取決于對(duì)應(yīng)的主鍵。

  • RelForeignKey
  • RelOneToOne
  • RelManyToMany
  • RelReverseOne
  • RelReverseMany
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)