databasegogo-gormpreloading

How to preload with Gorm


I am hitting a road block with preloading and associations

type Entity struct {
  ID           uint `gorm:"primary_key"`
  Username     string
  Repositories []*Repository `gorm:"many2many:entity_repositories"`
}

type Repository struct {
  ID       uint `gorm:"primary_key"`
  Name     string
  Entities []*Entity `gorm:"many2many:entity_repositories"`
} 

With small users numbers the preloading is fine using the below

 db.Preload("Repositories").Find(&list)

Also tried

 db.Model(&User{}).Related(&Repository{}, "Repositories").Find(&list)

The preload seems to a select * entities and then a inner join using a SELECT * FROM "repositories" INNER JOIN "entity_repositories" ON "entity_repositories"."repository_id" = "repositories"."id" WHERE ("entity_repositories"."entity_id" IN ('1','2','3','4','5','6','7','8','9','10'))

As the number of user's increases this is no longer maintainable as it hits a sqlite limit (dev). I've tried numerous permutations! .. Realistically i guess i just want it to do something like

SELECT entities.*, repositories.*  
FROM entities 
JOIN entity_repositories ON entity_repositories.entity_id = entities.id
JOIN repositories ON repositories.id = entity_repositories.repository_id
ORDER BY entities.id

And fill in the model for me ..

Am I doing something obviously wrong or?


Solution

  • Unfortunately that's just the way GORM handles preloading.

    go-pg has slightly better queries, but doesn't have the same functionality as GORM. It still will do multiple queries in some cases.

    I would honestly recommend just using query building with raw SQL, especially if you know what your models will look like at compile time. I ended up going with this approach in my project, despite the fact that I didn't know that my models were going to look like.