How to populate and embedded array with gorm?

南笙酒味 提交于 2021-02-07 10:56:17

问题


I have 2 structs with data like this:

type User struct {
  Pics Pic[]
}

type Pic struct {
  Id int   
  UserId int64
}

Although everytime I insert an User, Each of the pics are inserted on their table everytime I find the users, pics are not populated:

var users []User
db.Limit(pagesize).Where("updated_at > ?", date).Find(&users)

Am I doing something wrong?


回答1:


Your models (the structs) don't really make sense because User have a Pic array indicates a 'one to many' user to pics relationship however your user has no id property itself and there for cannot be related to items on the Pic table.

User should have a property Id which will be it's primary key and UserId is a foreign key on Pic that relates to it. Without the 'relation' between these two tables/entities there's no way you're going to return pics by querying users.

I'm not sure what all you need to do to make your code work since the example is incomplete but the first thing you need is an Id property which you should designate as a Primarykey with gorm annotations. You also should have annotations on the Pic struct saying UserId is a foreign key and Id is it's primary key.

Also, just fyi your array is not embedded. Embedding is a language feature which you're not using, if you embed the property it has no name and it's properties can be accessed directly from an instance of the embedding type.




回答2:


I had these issues once. Then I used Join function. See my example that works just fine:

type FileType struct {
     Id         int
}
type File struct {
     Id           int  
     FileType     `xorm:"extends"`
}

file := File{Id: id}


has, err := eng.
    Join("INNER", "FileType", "FileType.IdFileType = File.IdFileType").
    Get(&file)



回答3:


You probably know by now. You got to think as if you are creating a SQL table with 1-to-many relationship. Here is an example:

type Entry struct {
  ID int
  Name string
  ...
  ContainerID int
}

type Container struct {
  ID int
  Tag int
  Name string
  ...
  Entries []Entry `gorm:"foreignkey:ContainerID"`
}

The trick is to populate it. I am yet to find how to make it in one try. For every such dependency, you got to run something like:

c := getContainerFromDB(...)
if err := getROConn().Model(c).Related(&c.Entries, "Entries").Error; err != nil {
    return errors.Wrap(err, "error getting container field")
}



回答4:


Try Preload

db.Limit(pagesize).Where("updated_at > ?", date).Preload("Pics").Find(&users)


来源:https://stackoverflow.com/questions/28994759/how-to-populate-and-embedded-array-with-gorm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!