Your Question

I have this association

[A] 1-1 [B] 1-* [C]

I want all A with its B and every C for each B and tried with the following:

result := db.WithContext(ctx).
    Joins("B").
    Preload("B.C").
    Take(&A)

The thing is GORM makes 3 queries (A join B, Band C), but B was already loaded. Is this somehow expected?

The document you expected this should be explained

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

Expected answer

How to avoid unecessary queries to database for already loaded data.

Comment From: ccakes

Having a similar problem, would like to be able to eagerly fetch multi-layer nested objects without database round trips 👍

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days

Comment From: wregis

I wish I could better understand GORM sources, maybe I will find the time to do so in the future, for now I still would like to confirm this is not an intentional feature.

Comment From: jinzhu

Just use nested Preload, not going to support this for Join

Comment From: NilsJPWerner

Are you still against implementing this feature?

Comment From: jinzhu

pull request is welcome