Skip to content

Instantly share code, notes, and snippets.

@eoinahern
Created January 9, 2018 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinahern/1f8b54565c9ed995b5bb2941b6ffa8b6 to your computer and use it in GitHub Desktop.
Save eoinahern/1f8b54565c9ed995b5bb2941b6ffa8b6 to your computer and use it in GitHub Desktop.
updated models.
package models
type User struct {
UserName string `json: "username" gorm: "primary_key; type:TEXT"`
Verified bool `json: "verified" gorm: "type : "BOOLEAN" `
Password string `json: "password" gorm: "type: TEXT"`
Token string `json: "token" sql:"-" gorm:"-" `
Podcasts []Podcast `json: "podcasts" gorm: "ForeignKey:UserEmail"`
}
type UserTitle struct {
UserName string `json: "username"`
}
type Message struct {
Message string `json: "message"`
}
type Podcast struct {
PodcastID uint `gorm: "primary_key; AUTO_INCREMENT; type:INTEGER"`
UserEmail string `json: "useremail" gorm: "type:TEXT"`
Icon string `json: "icon" gorm: "type:TEXT"`
Name string `json: "name" gorm: "type: TEXT"`
EpisodeNum int `json: "episodenum" gorm: "type:INTEGER; default:0"`
Details string `json : "details" gorm: "type:TEXT"`
Episodes []Episode `json: "episodes" gorm: "ForeignKey:PodID"`
}
type Episode struct {
PodID uint `gorm: "type:INTEGER" json: "podid"`
Created string `json: "created" gorm: "type: TEXT"`
Updated string `json: "updated" gorm: "type: TEXT"`
URL string `json: "url" gorm: "type: TEXT"`
Downloads int32 `json: "downloads" gorm: "type: INTEGER; not null default:0"`
Blurb string `json: "blurb" gorm: "type: TEXT"`
}
//in my main i call the following.
conf := fmt.Sprintf("%s:%s@/%s", config.User, config.Password, config.Schema)
db, err := gorm.Open("mysql", conf)
if err != nil {
log.Fatal(err)
}
defer userDB.Close()
defer podcastDB.Close()
defer episodeDB.Close()
db.AutoMigrate(&models.User{}, &models.Podcast{}, &models.Episode{})
db.Model(&models.Podcast{}).AddForeignKey("user_email", "users(user_name)", "CASCADE", "CASCADE")
db.Model(&models.Episode{}).AddForeignKey("pod_id", "podcasts(podcast_id)", "CASCADE", "CASCADE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment