Postgresql gobuffalo tx.Eager().Create导致错误“;无法设置';%!s(int64=15)和#x27;至'&书信电报;无效的反射值>'”;
我正在尝试使用以下代码将数据写入具有关系的PostgreSQL数据库Postgresql gobuffalo tx.Eager().Create导致错误“;无法设置';%!s(int64=15)和#x27;至'&书信电报;无效的反射值>'”;,postgresql,go,buffalo,Postgresql,Go,Buffalo,我正在尝试使用以下代码将数据写入具有关系的PostgreSQL数据库 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.FormVa
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"}))
用户模型:
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"`
}
人物模型:
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
仅为用户模型创建一行。未创建人员模型的行。尝试打印时,在没有堆栈跟踪的情况下发生以下错误
无法设置“%”!s(int64=15)“到”
任何帮助都将不胜感激。请提供一个最小的可复制示例以帮助我们帮助您。请尝试将
UserId
更改为UserId
@mkopriva非常感谢。将UserId更改为UserId修复了该问题