Javier Manzano
Javier Manzano

Reputation: 4821

How to populate and embedded array with gorm?

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?

Upvotes: 0

Views: 9517

Answers (4)

msmaromi
msmaromi

Reputation: 535

Try Preload

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

Upvotes: 0

Schultz9999
Schultz9999

Reputation: 8936

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")
}

Upvotes: 0

evanmcdonnal
evanmcdonnal

Reputation: 48096

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.

Upvotes: 3

Felix Ribeiro
Felix Ribeiro

Reputation: 114

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)

Upvotes: 1

Related Questions