Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // models/gormModel.go
- type GormModel struct {
- ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
- CreatedAt *time.Time `json:"created_at,omitempty"`
- UpdatedAt *time.Time `json:"updated_at,omitempty"`
- }
- // models/user.go
- type User struct {
- GormModel
- Username string `gorm:"not null;unique" json:"username" form:"username" valid:"required~Username is required"`
- Email string `gorm:"not null;unique" json:"email" form:"email" valid:"required~Email is required,email~Invalid email format"`
- 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"`
- Age uint `gorm:"not null" json:"age" form:"age" valid:"required~Age is required,range(8|100)~Minimum age is 8 years old"`
- Photos []Photo `json:"photos,omitempty"`
- Comments []Comment `json:"comments,omitempty"`
- SocialMedias []SocialMedia `json:"social_medias,omitempty"`
- }
- // models/socialMedia.go
- type SocialMedia struct {
- GormModel
- Name string `gorm:"not null" json:"name" form:"name" valid:"required~Name is required"`
- SocialMediaURL string `gorm:"not null" json:"social_media_url" form:"social_media_url" valid:"required~Social Media URL is required"`
- UserID uint `json:"user_id"`
- User *User
- }
- // handler/controllers/socialMedias.go
- func CreateSocialMedia(c *gin.Context) {
- db = infra.GetDB()
- SocialMedia := models.SocialMedia{}
- userData := c.MustGet("userData").(jwt.MapClaims)
- userId := uint(userData["id"].(float64))
- contentType := utils.GetContentType(c)
- if contentType == appJson {
- c.ShouldBindJSON(&SocialMedia)
- } else {
- c.ShouldBind(&SocialMedia)
- }
- SocialMedia.UserID = userId
- err = db.Debug().Create(&SocialMedia).Error
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": "Bad Request",
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusCreated, SocialMedia)
- }
- // Line 47 above (err = db.Debug().Create(&SocialMedia).Error) throws this error:
- // ERROR: column "created_at" of relation "social_media" does not exist (SQLSTATE 42703)
Add Comment
Please, Sign In to add comment