douyu0845 2017-01-06 12:35
浏览 243
已采纳

在golang中与gorm一对多关系不起作用

So i got two tables

type Person struct {
ID int
FirstName string
LastName string
Functions []Function
}
type Function struct {
gorm.Model
Info string
Person Person
}

Now I create the tables like this

db.AutoMigrate(&models.Person{}, &models.Function{})

After this I init the db

user := models.Person{
    FirstName:            "Isa",
    LastName:  "istcool",
    Functions:          []models.Function{{Info: "Trainer"}, {Info: "CEO"}},
}
db.Create(&user)

Now the problem is that my Person table only got Firstname and Lastname column and my Function table only got info column. But when I start my GET request I get people with the column function which is always null.

http://imgur.com/74RU2Va (A screenshot from my GET request and my db) To see my whole programm visit my github repo: https://github.com/Gnadlinger/SpommunicateBackend.git

  • 写回答

1条回答 默认 最新

  • doudu5029 2017-01-08 20:53
    关注

    Finally found the answer!! The problem is my GET functions I have to use

    db.Preload("Functions").Find(&[]models.Person{})
    

    instead of

    db.Find(&[]models.Person{})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题