Guest User

Untitled

a guest
Jan 18th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. package models
  2.  
  3. type User struct {
  4. UserName string `json: "username" gorm: "primary_key; type:TEXT"`
  5. Verified bool `json: "verified" gorm: "type : "BOOLEAN" `
  6. Password string `json: "password" gorm: "type: TEXT"`
  7. Token string `json: "token" sql:"-" gorm:"-" `
  8. Podcasts []Podcast `json: "podcasts" gorm: "ForeignKey:UserEmail"`
  9. }
  10.  
  11. type UserTitle struct {
  12. UserName string `json: "username"`
  13. }
  14.  
  15. type Message struct {
  16. Message string `json: "message"`
  17. }
  18.  
  19. type Podcast struct {
  20. PodcastID uint `gorm: "primary_key; AUTO_INCREMENT; type:INTEGER"`
  21. UserEmail string `json: "useremail" gorm: "type:TEXT"`
  22. Icon string `json: "icon" gorm: "type:TEXT"`
  23. Name string `json: "name" gorm: "type: TEXT"`
  24. EpisodeNum int `json: "episodenum" gorm: "type:INTEGER; default:0"`
  25. Details string `json : "details" gorm: "type:TEXT"`
  26. Episodes []Episode `json: "episodes" gorm: "ForeignKey:PodID"`
  27. }
  28.  
  29. type Episode struct {
  30. PodID uint `gorm: "type:INTEGER" json: "podid"`
  31. Created string `json: "created" gorm: "type: TEXT"`
  32. Updated string `json: "updated" gorm: "type: TEXT"`
  33. URL string `json: "url" gorm: "type: TEXT"`
  34. Downloads int32 `json: "downloads" gorm: "type: INTEGER; not null default:0"`
  35. Blurb string `json: "blurb" gorm: "type: TEXT"`
  36. }
  37.  
  38. //in my main i call the following.
  39.  
  40. conf := fmt.Sprintf("%s:%s@/%s", config.User, config.Password, config.Schema)
  41. db, err := gorm.Open("mysql", conf)
  42.  
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46.  
  47. defer userDB.Close()
  48. defer podcastDB.Close()
  49. defer episodeDB.Close()
  50.  
  51. db.AutoMigrate(&models.User{}, &models.Podcast{}, &models.Episode{})
  52. db.Model(&models.Podcast{}).AddForeignKey("user_email", "users(user_name)", "CASCADE", "CASCADE")
  53. db.Model(&models.Episode{}).AddForeignKey("pod_id", "podcasts(podcast_id)", "CASCADE", "CASCADE")
Add Comment
Please, Sign In to add comment