gobuffalo / pop

A Tasty Treat For All Your Database Needs
MIT License
1.44k stars 243 forks source link

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

Open arrowak opened 4 years ago

arrowak commented 4 years ago

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 -

Actual Behavior

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 commented 4 years ago

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 commented 4 years ago

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.