Your Question
If this has already been answered i appologise, i couldn't find one in my search.
I am looking for a way to group preload queries to the same table. Take this model for example:
type Player struct {
gorm.Model
Level int
Xp int
LefthandId *uint `json:"-"`
Lefthand *InventoryItem
RighthandId *uint `json:"-"`
Righthand *InventoryItem
}
I can then load the player with its associations:
var player Player
_ = db.Preload(clause.Associations).First(&player, 1)
This loads in all the data as i would expect and produces the following queries:
SELECT * FROM `players` WHERE `players`.`id` = 1 AND `players`.`deleted_at` IS NULL;
SELECT * FROM `inventory_items` WHERE `inventory_items`.`id` = 1 AND `inventory_items`.`deleted_at` IS NULL;
SELECT * FROM `inventory_items` WHERE `inventory_items`.`id` = 2 AND `inventory_items`.`deleted_at` IS NULL;
I am hopeing there is a way to tell gorm to group the two queries to inventory_items so it produces the following queries:
SELECT * FROM `players` WHERE `players`.`id` = 1 AND `players`.`deleted_at` IS NULL;
SELECT * FROM `inventory_items` WHERE `inventory_items`.`id` IN(1, 2) AND `inventory_items`.`deleted_at` IS NULL;
The document you expected this should be explained
If this is possible then i would have expected it in the Associations Documentation or possibly the Has One Documentation
Expected answer
My guess is that this is not possible out of the box but thought it was worth an ask to save myself some time