日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

golang?gorm模型結構體的定義示例_Golang

作者:Jeff的技術棧 ? 更新時間: 2022-06-15 編程語言

1. 模型

1.1. 模型定義

type User struct {
    gorm.Model
    Birthday     time.Time
    Age          int
    Name         string  `gorm:"size:255"`       // string默認長度為255, 使用這種tag重設。
    Num          int     `gorm:"AUTO_INCREMENT"` // 自增
    CreditCard        CreditCard      // One-To-One (擁有一個 - CreditCard表的UserID作外鍵)
    Emails            []Email         // One-To-Many (擁有多個 - Email表的UserID作外鍵
    BillingAddress    Address         // One-To-One (屬于 - 本表的BillingAddressID作外鍵)
    BillingAddressID  sql.NullInt64
    ShippingAddress   Address         // One-To-One (屬于 - 本表的ShippingAddressID作外鍵)
    ShippingAddressID int
    IgnoreMe          int `gorm:"-"`   // 忽略這個字段
    Languages         []Language `gorm:"many2many:user_languages;"` // Many-To-Many , 'user_languages'是連接表
}
type Email struct {
    ID      int
    UserID  int     `gorm:"index"` // 外鍵 (屬于), tag `index`是為該列創建索引
    Email   string  `gorm:"type:varchar(100);unique_index"` // `type`設置sql類型, `unique_index` 為該列設置唯一索引
    Subscribed bool
}
type Address struct {
    ID       int
    Address1 string         `gorm:"not null;unique"` // 設置字段為非空并唯一
    Address2 string         `gorm:"type:varchar(100);unique"`
    Post     sql.NullString `gorm:"not null"`
}
type Language struct {
    ID   int
    Name string `gorm:"index:idx_name_code"` // 創建索引并命名,如果找到其他相同名稱的索引則創建組合索引
    Code string `gorm:"index:idx_name_code"` // `unique_index` also works
}
type CreditCard struct {
    gorm.Model
    UserID  uint
    Number  string
}

2. 約定

2.1. gorm.Model 結構體

基本模型定義gorm.Model,包括字段IDCreatedAtUpdatedAtDeletedAt,你可以將它嵌入你的模型,或者只寫你想要的字段

// 基本模型的定義
type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}
// 添加字段 `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`
type User struct {
  gorm.Model
  Name string
}
// 只需要字段 `ID`, `CreatedAt`
type User struct {
  ID        uint
  CreatedAt time.Time
  Name      string
}

2.2. 表名是結構體名稱的復數形式

type User struct {} // 默認表名是`users`
// 設置User的表名為`profiles`
func (User) TableName() string {
  return "profiles"
}
func (u User) TableName() string {
    if u.Role == "admin" {
        return "admin_users"
    } else {
        return "users"
    }
}
// 全局禁用表名復數
db.SingularTable(true) // 如果設置為true,`User`的默認表名為`user`,使用`TableName`設置的表名不受影響

2.3. 更改默認表名

您可以通過定義DefaultTableNameHandler對默認表名應用任何規則。

gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string  {
    return "prefix_" + defaultTableName;
}

2.4. 列名是字段名的蛇形小寫

type User struct {
  ID uint             // 列名為 `id`
  Name string         // 列名為 `name`
  Birthday time.Time  // 列名為 `birthday`
  CreatedAt time.Time // 列名為 `created_at`
}
// 重設列名
type Animal struct {
    AnimalId    int64     `gorm:"column:beast_id"`         // 設置列名為`beast_id`
    Birthday    time.Time `gorm:"column:day_of_the_beast"` // 設置列名為`day_of_the_beast`
    Age         int64     `gorm:"column:age_of_the_beast"` // 設置列名為`age_of_the_beast`
}

2.5. 字段ID為主鍵

type User struct {
  ID   uint  // 字段`ID`為默認主鍵
  Name string
}
// 使用tag`primary_key`用來設置主鍵
type Animal struct {
  AnimalId int64 `gorm:"primary_key"` // 設置AnimalId為主鍵
  Name     string
  Age      int64
}

2.6. 字段CreatedAt用于存儲記錄的創建時間

創建具有CreatedAt字段的記錄將被設置為當前時間

db.Create(&user) // 將會設置`CreatedAt`為當前時間
// 要更改它的值, 你需要使用`Update`
db.Model(&user).Update("CreatedAt", time.Now())

2.7. 字段UpdatedAt用于存儲記錄的修改時間

保存具有UpdatedAt字段的記錄將被設置為當前時間

db.Save(&user) // 將會設置`UpdatedAt`為當前時間
db.Model(&user).Update("name", "jinzhu") // 將會設置`UpdatedAt`為當前時間

2.8. 字段DeletedAt用于存儲記錄的刪除時間,如果字段存在

刪除具有DeletedAt字段的記錄,它不會沖數據庫中刪除,但只將字段DeletedAt設置為當前時間,并在查詢時無法找到記錄,請參閱[軟刪除]

原文鏈接:https://www.cnblogs.com/guyouyin123/p/14109877.html

欄目分類
最近更新