go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.57k stars 3.91k forks source link

How to lazy load relations in a slice of object? #5504

Open hasbai opened 2 years ago

hasbai commented 2 years ago

Your Question

Suppose I have two structs A and B, which are many to many related.

Now I have a slice of A []A, how to lazy load A.Bs for each A in []A?

The document you expected this should be explained

https://gorm.io/docs/preload.html

I know preload is intended to do this, but what I actually need is to lazy load the relations when needed, not at the beginning.

Expected answer

Is there a simply way to implement this feature? Thanks for your help.

hwbrzzl commented 1 year ago

Any update, please?

solnsumei commented 1 year ago

It would be better we are able to lazy load as the default.

apuatcfbd commented 6 months ago

This is very much necessary. Please take it seriously.

hsmfawaz commented 5 months ago

Eliminating redundant queries for fetching the main record is essential for optimizing performance and reducing resource overhead.

turkarshreyash commented 3 months ago

Any updates on this?