Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload seperate fields to the same model #5061

Open
indeedhat opened this issue Feb 9, 2022 · 0 comments
Open

Preload seperate fields to the same model #5061

indeedhat opened this issue Feb 9, 2022 · 0 comments
Assignees
Labels
type:feature_request feature request type:question general questions

Comments

@indeedhat
Copy link

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

@indeedhat indeedhat added the type:question general questions label Feb 9, 2022
@jinzhu jinzhu added the type:feature_request feature request label Apr 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants