Here is the model code for the same:
package models
import (
"errors"
"fmt"
"regexp"
"gorm.io/gorm"
)
type User struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
FirstName string `gorm:"column:first_name" json:"first_name"`
LastName string `gorm:"column:last_name" json:"last_name"`
Email string `gorm:"column:email" json:"email"`
Bio string `gorm:"column:bio" json:"bio"`
Blogs []Blog `gorm:"foreignKey:author_id"`
BlogReactions []BlogReaction `gorm:"foreignKey:user_id"`
CommentReactions []CommentReaction `gorm:"foreignKey:user_id"`
Comments []Comment `gorm:"foreignKey:user_id"`
Followers []User `gorm:"many2many:has_followers"`
}
func (*User) TableName() string {
return "user"
}
func (u *User) validate() (err error) {
fmt.Println(u)
if u.FirstName == "" {
return errors.New("first name cannot be empty")
}
emailMatched, _ := regexp.MatchString("^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", u.Email)
if !emailMatched {
return errors.New("invalid email format")
}
if len(u.Bio) > 200 {
return errors.New("bio too long")
}
return
}
func (u *User) BeforeSave(tx *gorm.DB) (err error) {
validationErr := u.validate()
return validationErr
}
I have a validation check before save. When I try to append a new follower:
dbErr := database.DB.
Model(&models.User{ID: uint(id)}).
Association("Followers").
Append(&models.User{ID: uint(follower_id)})
I get the validation error since the object has no information other than the ID:
first name cannot be empty
If the primary key is present in the object fed into Model() shouldn’t the other fields auto populate? How can I resolve this?