SHARE
TWEET

Untitled

a guest Feb 4th, 2016 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.  
  6.     "github.com/jinzhu/gorm"
  7.     _ "github.com/lib/pq"
  8. )
  9.  
  10. type Company struct {
  11.     gorm.Model
  12.     Name  string
  13.     Users []User
  14. }
  15.  
  16. type User struct {
  17.     gorm.Model
  18.     Company   Company
  19.     CompanyID uint   `gorm:"column:company_id"`
  20.     Email     string `gorm:"column:email"`
  21. }
  22.  
  23. func main() {
  24.     db, err := gorm.Open("postgres", "user=gorm password=123 dbname=gorm sslmode=disable")
  25.     if err != nil {
  26.         panic(err)
  27.     }
  28.  
  29.     db.CreateTable(&User{}, &Company{})
  30.  
  31.     // db.Create(&Company{
  32.     //  Users: []User{
  33.     //      {Email: "admin@google.com"},
  34.     //  },
  35.     // })
  36.  
  37.     db.LogMode(true)
  38.     user := User{}
  39.     db.Model(&user).Where("email = ?", "admin@google.com").Find(&user)
  40.     db.Model(&user).Related(&user.Company)
  41.     fmt.Println(user.ID)
  42.     fmt.Println(user.Company.ID)
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top