package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" ) //type PackageManagement struct { // Name string // Age int //} // // //type ListNode struct { // Val int // Next *ListNode //} // // //func reverseList(head *ListNode) *ListNode { // if head ==nil{ // return head // } // if head.Next ==nil{ // fmt.Println(head.Val) // return head // } // // resultNode := reverseList(head.Next) // head.Next.Next = head; // fmt.Println(head.Val) // head.Next = nil // return resultNode //} type User struct { Ids int64 Emails []Email `gorm:"ForeignKey:UserIds;AssociationForeignKey:Ids"` } type Email struct { Id int64 Email string UserIds uint } func (User)TableName() string { return "user" } func (Email)TableName()string{ return "email" } func main() { //head := ListNode{Val: 1,Next: &ListNode{Val: 2,Next: &ListNode{Val: 3,Next: nil}}} //reverseList(&head) db, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/lol?charset=utf8&parseTime=true&loc=Local") if err!=nil{ fmt.Printf(fmt.Sprint(err)) return } users := []User{} db.Preload("Emails").Find(&users) fmt.Printf("%+v\n",users) db.Preload("Emails").Find(&users) fmt.Printf("%+v",users) //result := PackageManagement{} //err =db.Table("package_management").Select("package_management.name,package_management.age").First(&result).Error //if err!=nil{ // fmt.Printf(fmt.Sprint(err)) // return //} //fmt.Printf("%+v",result) }
gorm的自定义主外键连接
最新推荐文章于 2024-03-12 09:35:23 发布