ahmpanji

Untitled

Mar 2nd, 2022 (edited)
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.20 KB | None | 0 0
  1. // models/gormModel.go
  2. type GormModel struct {
  3.     ID        uint       `gorm:"primaryKey;autoIncrement" json:"id"`
  4.     CreatedAt *time.Time `json:"created_at,omitempty"`
  5.     UpdatedAt *time.Time `json:"updated_at,omitempty"`
  6. }
  7.  
  8. // models/user.go
  9. type User struct {
  10.     GormModel
  11.     Username     string        `gorm:"not null;unique" json:"username" form:"username" valid:"required~Username is required"`
  12.     Email        string        `gorm:"not null;unique" json:"email" form:"email" valid:"required~Email is required,email~Invalid email format"`
  13.     Password     string        `gorm:"not null" json:"password" form:"password" valid:"required~Password is required,minstringlength(6)~Password has to have a minimum length of 6 characters"`
  14.     Age          uint          `gorm:"not null" json:"age" form:"age" valid:"required~Age is required,range(8|100)~Minimum age is 8 years old"`
  15.     Photos       []Photo       `json:"photos,omitempty"`
  16.     Comments     []Comment     `json:"comments,omitempty"`
  17.     SocialMedias []SocialMedia `json:"social_medias,omitempty"`
  18. }
  19.  
  20.  
  21. // models/socialMedia.go
  22. type SocialMedia struct {
  23.     GormModel
  24.     Name           string `gorm:"not null" json:"name" form:"name" valid:"required~Name is required"`
  25.     SocialMediaURL string `gorm:"not null" json:"social_media_url" form:"social_media_url" valid:"required~Social Media URL is required"`
  26.     UserID         uint   `json:"user_id"`
  27.     User           *User
  28. }
  29.  
  30. // handler/controllers/socialMedias.go
  31. func CreateSocialMedia(c *gin.Context) {
  32.     db = infra.GetDB()
  33.     SocialMedia := models.SocialMedia{}
  34.  
  35.     userData := c.MustGet("userData").(jwt.MapClaims)
  36.     userId := uint(userData["id"].(float64))
  37.  
  38.     contentType := utils.GetContentType(c)
  39.     if contentType == appJson {
  40.         c.ShouldBindJSON(&SocialMedia)
  41.     } else {
  42.         c.ShouldBind(&SocialMedia)
  43.     }
  44.  
  45.     SocialMedia.UserID = userId
  46.  
  47.     err = db.Debug().Create(&SocialMedia).Error
  48.     if err != nil {
  49.         c.JSON(http.StatusBadRequest, gin.H{
  50.             "error":   "Bad Request",
  51.             "message": err.Error(),
  52.         })
  53.         return
  54.     }
  55.  
  56.     c.JSON(http.StatusCreated, SocialMedia)
  57. }
  58.  
  59. // Line 47 above (err = db.Debug().Create(&SocialMedia).Error) throws this error:
  60. // ERROR: column "created_at" of relation "social_media" does not exist (SQLSTATE 42703)
Add Comment
Please, Sign In to add comment