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

Panic while retrieving inner associations via pointer based outer association #578

Open
arrowak opened this issue Jul 15, 2020 · 2 comments
Labels
bug Something isn't working f: associations the associations feature in pop s: triage Some tests need to be run to confirm the issue

Comments

@arrowak
Copy link

arrowak commented Jul 15, 2020

Description

The application panics when trying to eager load multi-level associations.

Steps to Reproduce the Problem

I have below model structure in my application.

organisation.go

type Organisation struct {
	ID            uuid.UUID `json:"id" db:"id"`
	Name          string    `json:"name" db:"name"`
	CreatedAt     time.Time `json:"created_at" db:"created_at"`
	UpdatedAt     time.Time `json:"updated_at" db:"updated_at"`

	Applications *Applications `json:"applications" has_many:"applications" db:"-"`
}

application.go

type Application struct {
	ID                uuid.UUID `json:"id" db:"id"`
	Name              string    `json:"name" db:"name"`
	ApplicationtypeID uuid.UUID `json:"applicationtype_id" db:"applicationtype_id"`
	OrganisationID    uuid.UUID `json:"organisation_id" db:"organisation_id"`
	CreatedAt         time.Time `json:"created_at" db:"created_at"`
	UpdatedAt         time.Time `json:"updated_at" db:"updated_at"`

	Organisation    *Organisation   `json:"organisation" belongs_to:"organisation" db:"-" `
	Applicationtype *Applicationtype `json:"applicationtype" belongs_to:"applicationtype" db:"-"`
}

applicationtype.go

type Applicationtype struct {
	ID        uuid.UUID `json:"id" db:"id"`
	Name      string    `json:"name" db:"name"`
	CreatedAt time.Time `json:"created_at" db:"created_at"`
	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

In the Show action of organisations.go, I am doing trying to eager load all the Applications for the current Organisation and also go another level deeper by eager loading Applicationtype for every Application found.

if err := tx.Eager("Applications.Applicationtype").Find(organisation, c.Param("organisation_id")); err != nil {
	return c.Error(http.StatusNotFound, err)
}

Expected Behavior

The statement should return -

  • All the details of the Organisation
  • All the details of all the Applications that belong to this Organisation
  • Applicationtype of each Application that was found in above step

Actual Behavior

  • It succeeds in getting the details of the Organisation
  • It succeeds in getting the details of all the Applications belonging to this Organisation
  • But fails to get Applicationtype for the above Applications, with following error -
could not retrieve associations: could not get struct associations: not a struct

The statement succeeds when I convert

Applications *Applications `json:"applications" has_many:"applications" db:"-"`

to

Applications Applications `json:"applications" has_many:"applications" db:"-"`

Info

OS: macOS Catalina v10.15.5
pop Version: 5.2.3 via Buffalo v0.16.10

@monfresh
Copy link

I'm seeing the same error in an application I'm working on. It's the exact same situation. We are trying to eager load "OriginDutyStation.Address" where OriginDutyStation is a pointer in the model.

@arrowak
Copy link
Author

arrowak commented Jul 23, 2020

Anything here people? For a large struct, It is not making sense to work on a copy rather than a pointer.

It would really help if you take a look into this.

@stanislas-m stanislas-m added bug Something isn't working f: associations the associations feature in pop labels Jul 28, 2020
@sio4 sio4 added the s: triage Some tests need to be run to confirm the issue label Sep 20, 2022
@sio4 sio4 added this to the v6.1.0 milestone Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working f: associations the associations feature in pop s: triage Some tests need to be run to confirm the issue
Projects
None yet
Development

No branches or pull requests

4 participants