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

Eager load on nested items only populates first nested item in the collection #123

Closed
jon-mckinney opened this issue Jun 13, 2018 · 9 comments
Labels
bug Something isn't working

Comments

@jon-mckinney
Copy link

I have the following model structure:

type BenefitCategory struct {
	ID              int64  `db:"id"`
	Name        string `db:"name"`

	Benefits []Benefit `many_to_many:"benefit_category_benefits"`
}

type Benefit struct {
	ID                      int64  `db:"id"`
	Name               string  `db:"name"`

	CancellationRuleID *int64 `db:"cancellation_rule_id"`
	CancellationRule CancellationRule `belongs_to:"cancellation_rule"`
}

type CancellationRule struct {
	ID               int64  `db:"id"`
	Name        string  `db:"name"`
}

When specifying eager loading on BenefitCategory for the nested Benefits.CancellationRule, I'm only seeing the nested values populated for the first BenefitCategory.

categories = &[]BenefitCategory[]
err := tx.Eager("Benefits.CancellationRule").All(categories)

Execution of the call above results in the data:

{
    "benefit_categories": [
        {
            "id": 37,
            "name": "Category 1",
            "benefits": [
                {
                    "id": 79,
                    "name": "Benefit 1",
                    "cancellation_rule_id": 5,
                    "cancellation_rule": {
                        "id": 5,
                        "name": "Strict"
                    }
                },
                {
                    "id": 80,
                    "name": "Benefit 2",
                    "cancellation_rule_id": 5,
                    "cancellation_rule": {
                        "id": 5,
                        "name": "Strict"
                    }
                }
            ]
        },
        {
            "id": 38,
            "name": "Category 2",
            "benefits": [
                {
                    "id": 83,
                    "name": "Benefit 3",
                    "cancellation_rule_id": 5,
                    "cancellation_rule": {
                        "id": 0,
                        "name": "",
                        "description": ""
                    }
                },
                {
                    "id": 85,
                    "name": "Benefit 4",
                    "cancellation_rule_id": 5,
                    "cancellation_rule": {
                        "id": 0,
                        "name": "",
                        "description": ""
                    }
                }
            ]
        }, ...
    ]
}

The nested cancellation_rule values are only populated for the first item in the top level collection of benefit_categories. Debug statements in the pop.eagerAssocations() method hint that subsequent items in the collection are not processed.

@robbyoconnor
Copy link

This is a limitation sadly...it will only populate one level.

@larrymjordan
Copy link
Member

@subsociety can you give us output log thrown for this issue?

@mclark4386
Copy link
Member

mclark4386 commented Jun 13, 2018

@robbyoconnor he is actually loading it correctly. Look at the pasted output. Specifically the cancellation_rules at the beginning versus the end.

@subsociety like @larrymjordan said please add any log output as well.

@jon-mckinney
Copy link
Author

@larrymjordan: Which output would you like to see?

@larrymjordan
Copy link
Member

larrymjordan commented Jun 13, 2018

@subsociety I would like to see if any error was raised while loading inner level. However, I could replicate it and you're correct about this. It seems this happens when loading nested associations with the has_many type. I'm working on that fix.

@larrymjordan larrymjordan added the bug Something isn't working label Jun 13, 2018
@jon-mckinney
Copy link
Author

Thanks @larrymjordan, I can verify that there are no errors output in the logs.

@robbyoconnor
Copy link

@mclark4386 ah so he is! I guess I didn't read it close enough -- sorry @subsociety !

@stanislas-m
Copy link
Member

Fixed with #124.

@jon-mckinney
Copy link
Author

Awesome fix, thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants