beego orm的使用


在使用beego model 去操作數據庫時 有一些疑惑  找到了一個比較好的博文

 

原文地址 : https://my.oschina.net/u/252343/blog/829912 (KelvinQ )侵刪 

模型定義

復雜的模型定義不是必須的,此功能用作數據庫數據轉換和自動建表

默認的表名規則,使用駝峰轉蛇形:

  1.  
    AuthUser -> auth_user
  2.  
    Auth_User -> auth__user
  3.  
    DB_AuthUser -> d_b__auth_user

除了開頭的大寫字母以外,遇到大寫會增加 _,原名稱中的下划線保留。

自定義表名

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

如果前綴設置prefix_那么表名為:prefix_auth_user

自定義索引

為單個或多個字段增加索引

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

自定義引擎

僅支持 MySQL

默認使用的引擎,為當前數據庫的默認引擎,這個是由你的 mysql 配置參數決定的。

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

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

設置參數

orm:"null;rel(fk)"

多個設置間使用 ; 分隔,設置的值如果是多個,使用 , 分隔。

忽略字段

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

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

auto

當 Field 類型為 int, int32, int64, uint, uint32, uint64 時,可以設置字段為自增健

  • 當模型定義里沒有主鍵時,符合上述類型且名稱為 Id 的 Field 將被視為自增健。

鑒於 go 目前的設計,即使使用了 uint64,但你也不能存儲到他的最大值。依然會作為 int64 處理。

參見 issue 6113

pk

設置為主鍵,適用於自定義其他類型為主鍵

null

數據庫表默認為 NOT NULL,設置 null 代表 ALLOW NULL

Name string `orm:"null"`

index

為單個字段增加索引

unique

為單個字段增加 unique 鍵

Name string `orm:"unique"`

column

為字段設置 db 字段的名稱

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

size

string 類型字段默認為 varchar(255)

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

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

digits / decimals

設置 float32, float64 類型的浮點精度

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

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

設置為 datetime 時,time.Time 字段的對應 db 類型使用 datetime

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

default

為字段設置默認值,類型必須符合(目前僅用於級聯刪除時的默認值)

  1.  
    type User struct {
  2.  
    ...
  3.  
    Status int `orm:"default(1)"`
  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 Post struct {
  2.  
    ...
  3.  
    User *User `orm:"rel(fk)"` // RelForeignKey relation
  4.  
    ...
  5.  
    }

對應的反向關系 RelReverseMany:

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

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.  
    ...
  5.  
    }

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.  
     
  12.  
    // 刪除 Profile 時將設置 User.Profile 的數據庫字段為 NULL

關於 on_delete 的相關例子

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

假設 Post -> User 是 ManyToOne 的關系,也就是外鍵。

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

這個時候即會刪除 Id 為 1 的 User 也會刪除其發布的 Post

不想刪除的話,需要設置 set_null

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

那這個時候,刪除 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 就可以了。

模型字段與數據庫類型的對應

在此列出 ORM 推薦的對應數據庫類型,自動建表功能也會以此為標准。

默認所有的字段都是 NOT NULL

MySQL

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

Sqlite3

go sqlite3
int, int32, int64, uint, uint32, uint64 - 設置 auto 或者名稱為 Id 時 integer AUTOINCREMENT
bool bool
string - 默認為 size 255 varchar(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

PostgreSQL

go postgres
int, int32, int64, uint, uint32, uint64 - 設置 auto 或者名稱為 Id 時 serial
bool bool
string - 默認為 size 255 varchar(size)
string - 設置 type(text) 時 text
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)

關系型字段

其字段類型取決於對應的主鍵。

  • RelForeignKey
  • RelOneToOne
  • RelManyToMany
  • RelReverseOne
  • RelReverseMany

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM