gorm的related理解和实例
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
var (
dev_db = "aplum:plum2016@tcp(localhost:3306)/aplum?charset=utf8"
local_db = "root:123456@tcp(localhost:3306)/test?charset=utf8"
)
type MyUser struct {
ID int `gorm:"TYPE:int(11);NOT NULL;PRIMARY_KEY;INDEX"`
Name string
}
type MyProfile struct {
ID int `gorm:"TYPE:int(11);NOT NULL;PRIMARY_KEY;INDEX"`
Name string
User MyUser `gorm:"foreignkey:UserRefer"`
UserRefer uint
}
func main() {
db, err := gorm.Open("mysql", local_db)
db.LogMode(true)
if err != nil {
panic(err)
}
defer db.Close()
related(db)
}
func setData(db *gorm.DB) {
db.Debug().AutoMigrate(&MyUser{})
db.Debug().AutoMigrate(&MyProfile{})
db.Debug().Create(&MyUser{ID: 1, Name: "uname1"})
db.Debug().Create(&MyUser{ID: 2, Name: "uname2"})
db.Debug().Create(&MyProfile{ID: 11, Name: "pname2", UserRefer: 1})
db.Debug().Create(&MyProfile{ID: 22, Name: "pname2", UserRefer: 2})
}
func related(db *gorm.DB) {
var profile MyProfile
db.Debug().First(&profile)
fmt.Println(fmt.Sprintf("%+v", profile))
db.Model(&profile).Related(&profile.User, "UserRefer")
fmt.Println(fmt.Sprintf("%+v", profile))
}
gorm的tag foreignkey就是图中的 foreignkey,
tag ASSOCIATION_FOREIGNKEY 就是图中的references。
定义外健约束的2个关键字,翻译到gorm的用法就是gorm:"FOREIGNKEY:UserId;ASSOCIATION_FOREIGNKEY:ID"
。