I'm trying to write data to PostgreSQL database with relationships using this code.
req := c.Request()
tx := c.Value("tx").(*pop.Connection)
user := &models.User{}
if req.FormValue("user_type") == "0" {
salt, _ := strconv.Atoi(os.Getenv("SALT"))
userType, _ := strconv.Atoi(req.FormValue("user_type"))
user.UserType = int16(userType)
user.Phone, _ = strconv.ParseInt(req.FormValue("phone"), 10, 64)
hash, _ := bcrypt.GenerateFromPassword([]byte(req.FormValue("password")), salt)
user.PassHash = string(hash)
user.Balance = 0
user.Validated = false
user.Person = &models.Person{
Name: req.FormValue("name"),
City: req.FormValue("city"),
}
} else {
}
err := tx.Eager().Create(user)
fmt.Println(errors.WithStack(err))
return c.Render(200, r.JSON(map[string]string{"message": "User successfully created"}))
User model:
type User struct {
ID int64 `db:"id" rw:"w" form:"-"`
UserType int16 `db:"user_type" form:"-"`
Phone int64 `db:"phone"`
PassHash string `db:"pass_hash" form:"-"`
Balance int64 `db:"balance" form:"-"`
Avatar nulls.String `db:"avatar" form:"-"`
Validated bool `db:"validated" form:"-"`
//Company *Company `has_one:"company" fk_id:"user_id"`
Person *Person `has_one:"person" fk_id:"user_id"`
}
Person model:
type Person struct {
ID int64 `db:"id"`
Name string `db:"name"`
Email nulls.String `db:"email"`
City string `db:"city"`
UserId int64 `db:"user_id"`
User *User `belongs_to:"user"`
}
tx.Eager().Create
is only creating a row for User model. Row for Person model is not being created. When trying to print, the following error occurs without a stack trace.
could not set '%!s(int64=15)' to '<invalid reflect.Value>'
Any help will be appreciated.