dpiz9879 2019-09-14 09:04
浏览 123

gobuffalo tx.Eager()。Create导致错误“无法将'%!s(int64 = 15)'设置为'<无效的reflect.Value>'”

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?