一对一
// one_to_one.go
package relate_tables
// 一对一
//type User struct {
// Id int
// Name string
// Age int
// Addr string
//
//}
//
//type UserProfile struct {
// Id int
// Pic string
// CPic string
// Phone string
// User User `gorm:"ForeignKey:UId;AssociationForeignKey:Id"` // 关联关系
// //UserID int // 默认关联字段
// UId int // u_id
//}
// 包含
type User struct {
Id int
Name string
Age int
Addr string
PId int
}
type UserProfile struct {
Id int
Pic string
CPic string
Phone string
User User `gorm:"ForeignKey:PId;AssociationForeignKey:Id"` // 关联关系
}
//一对一增加
userProfile := relate_tables.UserProfile{
Pic: "1.jpg",
CPic: "2.jpg",
Phone:"15625775964",
User: relate_tables.User{
Name: "jap",
Age: 32,
Addr: "珠海",
},
}
db.Create(&userProfile)