2016-11-08 09:24:09 +00:00
|
|
|
package test
|
2016-11-09 02:27:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"loafle.com/commons/orm/orm"
|
|
|
|
_ "loafle.com/commons/orm/orm/dialects/h2"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Member struct {
|
|
|
|
ID int64 `gorm:"primary_key"`
|
|
|
|
Profile Profile `gorm:"ForeignKey:ProfileID"`
|
|
|
|
ProfileID int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Profile struct {
|
|
|
|
ID int64 `gorm:"primary_key"`
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func db() *orm.DB {
|
|
|
|
db, err := orm.Open("h2", "host=192.168.1.215 port=5435 user=sa dbname=ttt sslmode=disable password=qwe123")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("ERR ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.LogMode(true)
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBelongTo(t *testing.T) {
|
|
|
|
|
|
|
|
db := db()
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
db.CreateTable(&Member{})
|
|
|
|
db.CreateTable(&Profile{})
|
|
|
|
|
|
|
|
m := Member{}
|
|
|
|
m.Profile.Name = "Geeeeek"
|
|
|
|
|
|
|
|
db.Create(&m)
|
|
|
|
|
|
|
|
}
|