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{})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化