1. 模型 1.1. 模型定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 type User struct { gorm.Model Birthday time.Time Age int Name string `gorm:"size:255"` Num int `gorm:"AUTO_INCREMENT"` CreditCard CreditCard Emails []Email BillingAddress Address BillingAddressID sql.NullInt64 ShippingAddress Address ShippingAddressID int IgnoreMe int `gorm:"-"` Languages []Language `gorm:"many2many:user_languages;"` }type Email struct { ID int UserID int `gorm:"index"` Email string `gorm:"type:varchar(100);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"` }type CreditCard struct { gorm.Model UserID uint Number string }
1.2. 约定 1.2.1. gorm.Model 结构体 基本模型定义gorm.Model
,包括字段ID,CreatedAt,UpdatedAt,DeletedAt,你可以将它嵌入你的模型,或者只写你想要的字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 type Model struct { ID uint `gorm:"primary_key"` CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time }type User struct { gorm.Model Name string }type User struct { ID uint CreatedAt time.Time Name string }
1.2.2. 表名是结构体名称的复数形式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 type User struct {} func (User) TableName () string { return "profiles" }func (u User) TableName () string { if u.Role == "admin" { return "admin_users" } else { return "users" } } db.SingularTable(true )
1.2.3. 更改默认表名 您可以通过定义DefaultTableNameHandler对默认表名应用任何规则。
1 2 3 gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string ) string { return "prefix_" + defaultTableName; }
1.2.4. 列名是字段名的蛇形小写 1 2 3 4 5 6 7 8 9 10 11 12 13 type User struct { ID uint Name string Birthday time.Time CreatedAt time.Time }type Animal struct { AnimalId int64 `gorm:"column:beast_id"` Birthday time.Time `gorm:"column:day_of_the_beast"` Age int64 `gorm:"column:age_of_the_beast"` }
1.2.5. 字段ID为主键 1 2 3 4 5 6 7 8 9 10 11 type User struct { ID uint Name string }type Animal struct { AnimalId int64 `gorm:"primary_key"` Name string Age int64 }
1.2.6. 字段CreatedAt用于存储记录的创建时间 创建具有CreatedAt
字段的记录将被设置为当前时间
1 2 3 4 db.Create(&user) db.Model(&user).Update("CreatedAt" , time.Now())
1.2.7. 字段UpdatedAt用于存储记录的修改时间 保存具有UpdatedAt
字段的记录将被设置为当前时间
1 2 db.Save(&user) db.Model(&user).Update("name" , "jinzhu" )
1.2.8. 字段DeletedAt用于存储记录的删除时间 如果字段存在,删除具有DeletedAt字段的记录,它不会冲数据库中删除,但只将字段DeletedAt设置为当前时间,并在查询时无法找到记录,请参阅软删除
1.3. 关联 1.3.1. 属于 1 2 3 4 5 6 7 8 9 10 11 12 13 14 type User struct { gorm.Model Profile Profile ProfileID int }type Profile struct { gorm.Model Name string } db.Model(&user).Related(&profile)
指定外键
1 2 3 4 5 6 7 8 9 10 type Profile struct { gorm.Model Name string }type User struct { gorm.Model Profile Profile `gorm:"ForeignKey:ProfileRefer"` ProfileRefer int }
指定外键和关联外键
1 2 3 4 5 6 7 8 9 10 11 type Profile struct { gorm.Model Refer string Name string }type User struct { gorm.Model Profile Profile `gorm:"ForeignKey:ProfileID;AssociationForeignKey:Refer"` ProfileID int }
1.3.2. 包含一个 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 type User struct { gorm.Model CreditCard CreditCard }type CreditCard struct { gorm.Model UserID uint Number string }var card CreditCard db.Model(&user).Related(&card, "CreditCard" ) db.Model(&user).Related(&card)
指定外键
1 2 3 4 5 6 7 8 9 10 type Profile struct { gorm.Model Name string UserRefer uint }type User struct { gorm.Model Profile Profile `gorm:"ForeignKey:UserRefer"` }
指定外键和关联外键
1 2 3 4 5 6 7 8 9 10 11 type Profile struct { gorm.Model Name string UserID uint }type User struct { gorm.Model Refer string Profile Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"` }
1.3.3. 包含多个 1 2 3 4 5 6 7 8 9 10 11 12 13 14 type User struct { gorm.Model Emails []Email }type Email struct { gorm.Model Email string UserID uint } db.Model(&user).Related(&emails)
指定外键
1 2 3 4 5 6 7 8 9 10 type Profile struct { gorm.Model Name string UserRefer uint }type User struct { gorm.Model Profiles []Profile `gorm:"ForeignKey:UserRefer"` }
指定外键和关联外键
1 2 3 4 5 6 7 8 9 10 11 type Profile struct { gorm.Model Name string UserID uint }type User struct { gorm.Model Refer string Profiles []Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"` }
1.3.4. 多对多 1 2 3 4 5 6 7 8 9 10 11 12 13 type User struct { gorm.Model Languages []Language `gorm:"many2many:user_languages;"` }type Language struct { gorm.Model Name string } db.Model(&user).Related(&languages, "Languages" )
指定外键和关联外键
1 2 3 4 5 6 7 8 9 type CustomizePerson struct { IdPerson string `gorm:"primary_key:true"` Accounts []CustomizeAccount `gorm:"many2many:PersonAccount;ForeignKey:IdPerson;AssociationForeignKey:IdAccount"` }type CustomizeAccount struct { IdAccount string `gorm:"primary_key:true"` Name string }
1.3.5 多种包含 支持多种的包含一个和包含多个的关联
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 type Cat struct { Id int Name string Toy Toy `gorm:"polymorphic:Owner;"` } type Dog struct { Id int Name string Toy Toy `gorm:"polymorphic:Owner;"` } type Toy struct { Id int Name string OwnerId int OwnerType string }
注意:多态属性和多对多显式不支持,并且会抛出错误。
1.3.6. 关联模式 关联模式包含一些帮助方法来处理关系事情很容易。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 var user User db.Model(&user).Association("Languages" ) db.Model(&user).Association("Languages" ).Find(&languages) db.Model(&user).Association("Languages" ).Append([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages" ).Append(Language{Name: "DE" }) db.Model(&user).Association("Languages" ).Delete([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages" ).Delete(languageZH, languageEN) db.Model(&user).Association("Languages" ).Replace([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages" ).Replace(Language{Name: "DE" }, languageEN) db.Model(&user).Association("Languages" ).Count() db.Model(&user).Association("Languages" ).Clear()